View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.stream;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.apache.mina.core.buffer.IoBuffer;
26  import org.apache.mina.core.filterchain.IoFilter;
27  import org.apache.mina.core.session.IoSession;
28  import org.apache.mina.core.write.WriteRequest;
29  
30  /**
31   * Filter implementation which makes it possible to write {@link InputStream}
32   * objects directly using {@link IoSession#write(Object)}. When an
33   * {@link InputStream} is written to a session this filter will read the bytes
34   * from the stream into {@link IoBuffer} objects and write those buffers
35   * to the next filter. When end of stream has been reached this filter will
36   * call {@link IoFilter.NextFilter#messageSent(IoSession,WriteRequest)} using the original
37   * {@link InputStream} written to the session and notifies
38   * {@link org.apache.mina.core.future.WriteFuture} on the
39   * original {@link org.apache.mina.core.write.WriteRequest}.
40   * <p/>
41   * This filter will ignore written messages which aren't {@link InputStream}
42   * instances. Such messages will be passed to the next filter directly.
43   * </p>
44   * <p/>
45   * NOTE: this filter does not close the stream after all data from stream
46   * has been written. The {@link org.apache.mina.core.service.IoHandler} should take
47   * care of that in its
48   * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)}
49   * callback.
50   * </p>
51   *
52   * @author The Apache MINA Project (dev@mina.apache.org)
53   * @version $Rev: 689337 $, $Date: 2008-08-27 04:18:17 +0200 (Wed, 27 Aug 2008) $
54   * @org.apache.xbean.XBean
55   */
56  public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> {
57  
58      @Override
59      protected IoBuffer getNextBuffer(InputStream is) throws IOException {
60          byte[] bytes = new byte[getWriteBufferSize()];
61  
62          int off = 0;
63          int n = 0;
64          while (off < bytes.length
65                  && (n = is.read(bytes, off, bytes.length - off)) != -1) {
66              off += n;
67          }
68  
69          if (n == -1 && off == 0) {
70              return null;
71          }
72  
73          IoBuffer buffer = IoBuffer.wrap(bytes, 0, off);
74  
75          return buffer;
76      }
77      
78      @Override
79      protected Class<InputStream> getMessageClass() {
80      	return InputStream.class;
81      }
82  
83  }