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 IoFilter.NextFilter#messageSent(IoSession,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
56 AbstractStreamWriteFilter<FileRegion> {
57
58 @Override
59 protected Class<FileRegion> getMessageClass() {
60 return FileRegion.class;
61 }
62
63 @Override
64 protected IoBuffer getNextBuffer(FileRegion fileRegion) throws IOException {
65 // If there are no more bytes to read, return null
66 if (fileRegion.getRemainingBytes() <= 0) {
67 return null;
68 }
69
70 // Allocate the buffer for reading from the file
71 final int bufferSize = (int) Math.min(getWriteBufferSize(), fileRegion.getRemainingBytes());
72 IoBuffer buffer = IoBuffer.allocate(bufferSize);
73
74 // Read from the file
75 int bytesRead = fileRegion.getFileChannel().read(buffer.buf(),
76 fileRegion.getPosition());
77 fileRegion.update(bytesRead);
78
79 // return the buffer
80 buffer.flip();
81 return buffer;
82 }
83
84 }