1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.jboss.netty.handler.codec.marshalling;
17  
18  import java.io.IOException;
19  
20  import org.jboss.marshalling.ByteInput;
21  import org.jboss.netty.buffer.ChannelBuffer;
22  
23  
24  
25  
26  
27  
28  class ChannelBufferByteInput implements ByteInput {
29  
30      private final ChannelBuffer buffer;
31  
32      public ChannelBufferByteInput(ChannelBuffer buffer) {
33          this.buffer = buffer;
34      }
35  
36      public void close() throws IOException {
37          
38      }
39  
40      public int available() throws IOException {
41          return buffer.readableBytes();
42      }
43  
44      public int read() throws IOException {
45          if (buffer.readable()) {
46              return buffer.readByte() & 0xff;
47          }
48          return -1;
49      }
50  
51      public int read(byte[] array) throws IOException {
52          return read(array, 0, array.length);
53      }
54  
55      public int read(byte[] dst, int dstIndex, int length) throws IOException {
56          int available = available();
57          if (available == 0) {
58              return -1;
59          }
60  
61          length = Math.min(available, length);
62          buffer.readBytes(dst, dstIndex, length);
63          return length;
64      }
65  
66      public long skip(long bytes) throws IOException {
67          int readable = buffer.readableBytes();
68          if (readable < bytes) {
69              bytes = readable;
70          }
71          buffer.readerIndex((int) (buffer.readerIndex() + bytes));
72          return bytes;
73      }
74  
75  }