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  
24  import org.apache.mina.core.buffer.IoBuffer;
25  import org.apache.mina.core.file.FileRegion;
26  import org.apache.mina.core.session.IoSession;
27  
28  /**
29   * Filter implementation that converts a {@link FileRegion} to {@link IoBuffer}
30   * objects and writes those buffers to the next filter. When end of the
31   * {@code FileRegion} has been reached this filter will call
32   * {@link org.apache.mina.core.filterchain.IoFilter.NextFilter#messageSent(org.apache.mina.core.session.IoSession, org.apache.mina.core.write.WriteRequest)} using the
33   * original {@link FileRegion} written to the session and notifies
34   * {@link org.apache.mina.core.future.WriteFuture} on the original
35   * {@link org.apache.mina.core.write.WriteRequest}.
36   * <p>Normall {@code FileRegion} objects should be handled by the
37   * {@link org.apache.mina.core.service.IoProcessor} but this is not always possible
38   * if a filter is being used that needs to modify the contents of the file
39   * before sending over the network (i.e. the
40   * {@link org.apache.mina.filter.ssl.SslFilter} or a data compression filter.)
41   * </p>
42   * <p> This filter will ignore written messages which aren't {@link FileRegion}
43   * instances. Such messages will be passed to the next filter directly.
44   * </p>
45   * <p><b>NOTE:</b> this filter does not close the file channel in
46   * {@link FileRegion#getFileChannel()} after the data from the file has been
47   * written.  The {@code FileChannel} should be closed in either 
48   * {@link org.apache.mina.core.service.IoHandler#messageSent(IoSession,Object)}
49   * or in an {@link org.apache.mina.core.future.IoFutureListener} associated with the
50   * {@code WriteFuture}.
51   * </p>
52   *
53   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
54   * @org.apache.xbean.XBean
55   */
56  public class FileRegionWriteFilter extends AbstractStreamWriteFilter<FileRegion> {
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      protected Class<FileRegion> getMessageClass() {
62          return FileRegion.class;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      protected IoBuffer getNextBuffer(FileRegion fileRegion) throws IOException {
70          // If there are no more bytes to read, return null
71          if (fileRegion.getRemainingBytes() <= 0) {
72              return null;
73          }
74  
75          // Allocate the buffer for reading from the file
76          int bufferSize = (int) Math.min(getWriteBufferSize(), fileRegion.getRemainingBytes());
77          IoBuffer buffer = IoBuffer.allocate(bufferSize);
78  
79          // Read from the file
80          int bytesRead = fileRegion.getFileChannel().read(buffer.buf(), fileRegion.getPosition());
81          fileRegion.update(bytesRead);
82  
83          // return the buffer
84          buffer.flip();
85          
86          return buffer;
87      }
88  }