1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 *19 */20package org.apache.mina.filter.stream;
2122import java.io.IOException;
2324import org.apache.mina.core.buffer.IoBuffer;
25import org.apache.mina.core.file.FileRegion;
2627/**28 * Filter implementation that converts a {@link FileRegion} to {@link IoBuffer}29 * objects and writes those buffers to the next filter. When end of the30 * {@code FileRegion} has been reached this filter will call31 * {@link org.apache.mina.core.filterchain.IoFilter.NextFilter#messageSent(org.apache.mina.core.session.IoSession, org.apache.mina.core.write.WriteRequest)} using the32 * original {@link FileRegion} written to the session and notifies33 * {@link org.apache.mina.core.future.WriteFuture} on the original34 * {@link org.apache.mina.core.write.WriteRequest}.35 * <p>Normall {@code FileRegion} objects should be handled by the36 * {@link org.apache.mina.core.service.IoProcessor} but this is not always possible37 * if a filter is being used that needs to modify the contents of the file38 * before sending over the network (i.e. the39 * {@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 in45 * {@link FileRegion#getFileChannel()} after the data from the file has been46 * 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 the49 * {@code WriteFuture}.50 * </p>51 *52 * @author <a href="http://mina.apache.org">Apache MINA Project</a>53 * @org.apache.xbean.XBean54 */55publicclassFileRegionWriteFilterextends AbstractStreamWriteFilter<FileRegion> {
5657 @Override
58protected Class<FileRegion> getMessageClass() {
59return FileRegion.class;
60 }
6162 @Override
63protectedIoBuffer getNextBuffer(FileRegion fileRegion) throws IOException {
64// If there are no more bytes to read, return null65if (fileRegion.getRemainingBytes() <= 0) {
66returnnull;
67 }
6869// Allocate the buffer for reading from the file70finalint bufferSize = (int) Math.min(getWriteBufferSize(), fileRegion.getRemainingBytes());
71IoBuffer buffer = IoBuffer.allocate(bufferSize);
7273// Read from the file74int bytesRead = fileRegion.getFileChannel().read(buffer.buf(), fileRegion.getPosition());
75 fileRegion.update(bytesRead);
7677// return the buffer78 buffer.flip();
79return buffer;
80 }
8182 }