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