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  
28  /**
29   * Filter implementation which makes it possible to write {@link InputStream}
30   * objects directly using {@link IoSession#write(Object)}. When an
31   * {@link InputStream} is written to a session this filter will read the bytes
32   * from the stream into {@link IoBuffer} objects and write those buffers
33   * to the next filter. When end of stream has been reached this filter will
34   * call {@link IoFilter.NextFilter#messageSent(IoSession,WriteRequest)} using the original
35   * {@link InputStream} written to the session and notifies
36   * {@link org.apache.mina.core.future.WriteFuture} on the
37   * original {@link org.apache.mina.core.write.WriteRequest}.
38   * <p/>
39   * This filter will ignore written messages which aren't {@link InputStream}
40   * instances. Such messages will be passed to the next filter directly.
41   * </p>
42   * <p/>
43   * NOTE: this filter does not close the stream after all data from stream
44   * has been written. The {@link org.apache.mina.core.service.IoHandler} should take
45   * care of that in its
46   * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)}
47   * callback.
48   * </p>
49   *
50   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
51   * @org.apache.xbean.XBean
52   */
53  public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> {
54  
55      @Override
56      protected IoBuffer getNextBuffer(InputStream is) throws IOException {
57          byte[] bytes = new byte[getWriteBufferSize()];
58  
59          int off = 0;
60          int n = 0;
61          while (off < bytes.length && (n = is.read(bytes, off, bytes.length - off)) != -1) {
62              off += n;
63          }
64  
65          if (n == -1 && off == 0) {
66              return null;
67          }
68  
69          IoBuffer buffer = IoBuffer.wrap(bytes, 0, off);
70  
71          return buffer;
72      }
73  
74      @Override
75      protected Class<InputStream> getMessageClass() {
76          return InputStream.class;
77      }
78  
79  }