View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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 io.netty.handler.stream;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.channel.ChannelHandlerContext;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.channels.ReadableByteChannel;
24  
25  /**
26   * A {@link ChunkedInput} that fetches data from a {@link ReadableByteChannel}
27   * chunk by chunk.  Please note that the {@link ReadableByteChannel} must
28   * operate in blocking mode.  Non-blocking mode channels are not supported.
29   */
30  public class ChunkedNioStream implements ChunkedInput<ByteBuf> {
31  
32      private final ReadableByteChannel in;
33  
34      private final int chunkSize;
35      private long offset;
36  
37      /**
38       * Associated ByteBuffer
39       */
40      private final ByteBuffer byteBuffer;
41  
42      /**
43       * Creates a new instance that fetches data from the specified channel.
44       */
45      public ChunkedNioStream(ReadableByteChannel in) {
46          this(in, ChunkedStream.DEFAULT_CHUNK_SIZE);
47      }
48  
49      /**
50       * Creates a new instance that fetches data from the specified channel.
51       *
52       * @param chunkSize the number of bytes to fetch on each
53       *                  {@link #readChunk(ChannelHandlerContext)} call
54       */
55      public ChunkedNioStream(ReadableByteChannel in, int chunkSize) {
56          if (in == null) {
57              throw new NullPointerException("in");
58          }
59          if (chunkSize <= 0) {
60              throw new IllegalArgumentException("chunkSize: " + chunkSize +
61                      " (expected: a positive integer)");
62          }
63          this.in = in;
64          offset = 0;
65          this.chunkSize = chunkSize;
66          byteBuffer = ByteBuffer.allocate(chunkSize);
67      }
68  
69      /**
70       * Returns the number of transferred bytes.
71       */
72      public long transferredBytes() {
73          return offset;
74      }
75  
76      @Override
77      public boolean isEndOfInput() throws Exception {
78          if (byteBuffer.position() > 0) {
79              // A previous read was not over, so there is a next chunk in the buffer at least
80              return false;
81          }
82          if (in.isOpen()) {
83              // Try to read a new part, and keep this part (no rewind)
84              int b = in.read(byteBuffer);
85              if (b < 0) {
86                  return true;
87              } else {
88                  offset += b;
89                  return false;
90              }
91          }
92          return true;
93      }
94  
95      @Override
96      public void close() throws Exception {
97          in.close();
98      }
99  
100     @Deprecated
101     @Override
102     public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
103         return readChunk(ctx.alloc());
104     }
105 
106     @Override
107     public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
108         if (isEndOfInput()) {
109             return null;
110         }
111         // buffer cannot be not be empty from there
112         int readBytes = byteBuffer.position();
113         for (;;) {
114             int localReadBytes = in.read(byteBuffer);
115             if (localReadBytes < 0) {
116                 break;
117             }
118             readBytes += localReadBytes;
119             offset += localReadBytes;
120             if (readBytes == chunkSize) {
121                 break;
122             }
123         }
124         byteBuffer.flip();
125         boolean release = true;
126         ByteBuf buffer = allocator.buffer(byteBuffer.remaining());
127         try {
128             buffer.writeBytes(byteBuffer);
129             byteBuffer.clear();
130             release = false;
131             return buffer;
132         } finally {
133             if (release) {
134                 buffer.release();
135             }
136         }
137     }
138 
139     @Override
140     public long length() {
141         return -1;
142     }
143 
144     @Override
145     public long progress() {
146         return offset;
147     }
148 }