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