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