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.session.IoSession;
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 org.apache.mina.core.filterchain.IoFilter.NextFilter#messageSent(org.apache.mina.core.session.IoSession, org.apache.mina.core.write.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   * NOTE: this filter does not close the stream after all data from stream
43   * has been written. The {@link org.apache.mina.core.service.IoHandler} should take
44   * care of that in its
45   * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)}
46   * callback.
47   *
48   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
49   * @org.apache.xbean.XBean
50   */
51  public class StreamWriteFilter extends AbstractStreamWriteFilter<InputStream> {
52      /**
53       * {@inheritDoc}
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          
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          return IoBuffer.wrap(bytes, 0, off);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected Class<InputStream> getMessageClass() {
78          return InputStream.class;
79      }
80  }