This project has retired. For details please refer to its Attic page.
DynamicChannelBufferInputStream xref
View Javadoc

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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.utils;
19  
20  import java.io.DataInput;
21  import java.io.IOException;
22  import java.io.UTFDataFormatException;
23  import io.netty.buffer.ByteBuf;
24  
25  /**
26   * Special input that reads from a DynamicChannelBuffer.
27   */
28  public class DynamicChannelBufferInputStream implements DataInput {
29    /** Internal dynamic channel buffer */
30    private ByteBuf buffer;
31  
32    /**
33     * Constructor.
34     *
35     * @param buffer Buffer to read from
36     */
37    public DynamicChannelBufferInputStream(ByteBuf buffer) {
38      this.buffer = buffer;
39    }
40  
41    @Override
42    public void readFully(byte[] b) throws IOException {
43      buffer.readBytes(b);
44    }
45  
46    @Override
47    public void readFully(byte[] b, int off, int len) throws IOException {
48      buffer.readBytes(b, off, len);
49    }
50  
51    @Override
52    public int skipBytes(int n) throws IOException {
53      buffer.skipBytes(n);
54      return n;
55    }
56  
57    @Override
58    public boolean readBoolean() throws IOException {
59      int ch = buffer.readByte();
60      if (ch < 0) {
61        throw new IllegalStateException("readBoolean: Got " + ch);
62      }
63      return ch != 0;
64    }
65  
66    @Override
67    public byte readByte() throws IOException {
68      return buffer.readByte();
69    }
70  
71    @Override
72    public int readUnsignedByte() throws IOException {
73      return buffer.readUnsignedByte();
74    }
75  
76    @Override
77    public short readShort() throws IOException {
78      return buffer.readShort();
79    }
80  
81    @Override
82    public int readUnsignedShort() throws IOException {
83      return buffer.readUnsignedShort();
84    }
85  
86    @Override
87    public char readChar() throws IOException {
88      return buffer.readChar();
89    }
90  
91    @Override
92    public int readInt() throws IOException {
93      return buffer.readInt();
94    }
95  
96    @Override
97    public long readLong() throws IOException {
98      return buffer.readLong();
99    }
100 
101   @Override
102   public float readFloat() throws IOException {
103     return buffer.readFloat();
104   }
105 
106   @Override
107   public double readDouble() throws IOException {
108     return buffer.readDouble();
109   }
110 
111   @Override
112   public String readLine() throws IOException {
113     // Note that this code is mostly copied from DataInputStream
114     char[] buf = new char[128];
115 
116     int room = buf.length;
117     int offset = 0;
118     int c;
119 
120   loop:
121     while (true) {
122       c = buffer.readByte();
123       switch (c) {
124       case -1:
125       case '\n':
126         break loop;
127       case '\r':
128         int c2 = buffer.readByte();
129         if ((c2 != '\n') && (c2 != -1)) {
130           buffer.readerIndex(buffer.readerIndex() - 1);
131         }
132         break loop;
133       default:
134         if (--room < 0) {
135           char[] replacebuf = new char[offset + 128];
136           room = replacebuf.length - offset - 1;
137           System.arraycopy(buf, 0, replacebuf, 0, offset);
138           buf = replacebuf;
139         }
140         buf[offset++] = (char) c;
141         break;
142       }
143     }
144     if ((c == -1) && (offset == 0)) {
145       return null;
146     }
147     return String.copyValueOf(buf, 0, offset);
148   }
149 
150   @Override
151   public String readUTF() throws IOException {
152     // Note that this code is mostly copied from DataInputStream
153     int utflen = buffer.readUnsignedShort();
154 
155     byte[] bytearr = new byte[utflen];
156     char[] chararr = new char[utflen];
157 
158     int c;
159     int char2;
160     int char3;
161     int count = 0;
162     int chararrCount = 0;
163 
164     buffer.readBytes(bytearr, 0, utflen);
165 
166     while (count < utflen) {
167       c = (int) bytearr[count] & 0xff;
168       if (c > 127) {
169         break;
170       }
171       count++;
172       chararr[chararrCount++] = (char) c;
173     }
174 
175     while (count < utflen) {
176       c = (int) bytearr[count] & 0xff;
177       switch (c >> 4) {
178       case 0:
179       case 1:
180       case 2:
181       case 3:
182       case 4:
183       case 5:
184       case 6:
185       case 7:
186         /* 0xxxxxxx */
187         count++;
188         chararr[chararrCount++] = (char) c;
189         break;
190       case 12:
191       case 13:
192         /* 110x xxxx   10xx xxxx*/
193         count += 2;
194         if (count > utflen) {
195           throw new UTFDataFormatException(
196               "malformed input: partial character at end");
197         }
198         char2 = (int) bytearr[count - 1];
199         if ((char2 & 0xC0) != 0x80) {
200           throw new UTFDataFormatException(
201                 "malformed input around byte " + count);
202         }
203         chararr[chararrCount++] = (char) (((c & 0x1F) << 6) |
204             (char2 & 0x3F));
205         break;
206       case 14:
207         /* 1110 xxxx  10xx xxxx  10xx xxxx */
208         count += 3;
209         if (count > utflen) {
210           throw new UTFDataFormatException(
211               "malformed input: partial character at end");
212         }
213         char2 = (int) bytearr[count - 2];
214         char3 = (int) bytearr[count - 1];
215         if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
216           throw new UTFDataFormatException(
217               "malformed input around byte " + (count - 1));
218         }
219         chararr[chararrCount++] = (char) (((c & 0x0F) << 12) |
220             ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
221         break;
222       default:
223         /* 10xx xxxx,  1111 xxxx */
224         throw new UTFDataFormatException(
225             "malformed input around byte " + count);
226       }
227     }
228     // The number of chars produced may be less than utflen
229     return new String(chararr, 0, chararrCount);
230   }
231 }