View Javadoc

1   /*
2    * Copyright 2010 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel;
17  
18  import java.io.IOException;
19  import java.nio.channels.FileChannel;
20  import java.nio.channels.WritableByteChannel;
21  
22  import org.jboss.netty.util.ExternalResourceReleasable;
23  
24  /**
25   * A region of a file that is sent via a {@link Channel} which supports
26   * <a href="http://en.wikipedia.org/wiki/Zero-copy">zero-copy file transfer</a>.
27   *
28   * <h3>Upgrade your JDK / JRE</h3>
29   *
30   * {@link FileChannel#transferTo(long, long, WritableByteChannel)} has at least
31   * four known bugs in the old versions of Sun JDK and perhaps its derived ones.
32   * Please upgrade your JDK to 1.6.0_18 or later version if you are going to use
33   * zero-copy file transfer.
34   * <ul>
35   * <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103988">5103988</a>
36   *   - FileChannel.transferTo() should return -1 for EAGAIN instead throws IOException</li>
37   * <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6253145">6253145</a>
38   *   - FileChannel.transferTo() on Linux fails when going beyond 2GB boundary</li>
39   * <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6427312">6427312</a>
40   *   - FileChannel.transferTo() throws IOException "system call interrupted"</li>
41   * <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6524172">6470086</a>
42   *   - FileChannel.transferTo(2147483647, 1, channel) causes "Value too large" exception</li>
43   * </ul>
44   *
45   * <h3>Check your operating system and JDK / JRE</h3>
46   *
47   * If your operating system (or JDK / JRE) does not support zero-copy file
48   * transfer, sending a file with {@link FileRegion} might fail or yield worse
49   * performance.  For example, sending a large file doesn't work well in Windows.
50   *
51   * <h3>Not all transports support it</h3>
52   *
53   * Currently, the NIO transport is the only transport that supports {@link FileRegion}.
54   * Attempting to write a {@link FileRegion} to non-NIO {@link Channel} will trigger
55   * a {@link ClassCastException} or a similar exception.
56   *
57   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
58   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
59   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
60   */
61  public interface FileRegion extends ExternalResourceReleasable {
62  
63      // FIXME Make sure all transports support writing a FileRegion
64      //       Even if zero copy cannot be achieved, all transports should emulate it.
65  
66      /**
67       * Returns the offset in the file where the transfer began.
68       */
69      long getPosition();
70  
71      /**
72       * Returns the number of bytes to transfer.
73       */
74      long getCount();
75  
76      /**
77       * Transfers the content of this file region to the specified channel.
78       *
79       * @param target    the destination of the transfer
80       * @param position  the relative offset of the file where the transfer
81       *                  begins from.  For example, <tt>0</tt> will make the
82       *                  transfer start from {@link #getPosition()}th byte and
83       *                  <tt>{@link #getCount()} - 1</tt> will make the last
84       *                  byte of the region transferred.
85       */
86      long transferTo(WritableByteChannel target, long position) throws IOException;
87  }