Coverage Report - org.apache.giraph.utils.DynamicChannelBufferInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
DynamicChannelBufferInputStream
0%
0/78
0%
0/50
3.25
 
 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  0
   public DynamicChannelBufferInputStream(ByteBuf buffer) {
 38  0
     this.buffer = buffer;
 39  0
   }
 40  
 
 41  
   @Override
 42  
   public void readFully(byte[] b) throws IOException {
 43  0
     buffer.readBytes(b);
 44  0
   }
 45  
 
 46  
   @Override
 47  
   public void readFully(byte[] b, int off, int len) throws IOException {
 48  0
     buffer.readBytes(b, off, len);
 49  0
   }
 50  
 
 51  
   @Override
 52  
   public int skipBytes(int n) throws IOException {
 53  0
     buffer.skipBytes(n);
 54  0
     return n;
 55  
   }
 56  
 
 57  
   @Override
 58  
   public boolean readBoolean() throws IOException {
 59  0
     int ch = buffer.readByte();
 60  0
     if (ch < 0) {
 61  0
       throw new IllegalStateException("readBoolean: Got " + ch);
 62  
     }
 63  0
     return ch != 0;
 64  
   }
 65  
 
 66  
   @Override
 67  
   public byte readByte() throws IOException {
 68  0
     return buffer.readByte();
 69  
   }
 70  
 
 71  
   @Override
 72  
   public int readUnsignedByte() throws IOException {
 73  0
     return buffer.readUnsignedByte();
 74  
   }
 75  
 
 76  
   @Override
 77  
   public short readShort() throws IOException {
 78  0
     return buffer.readShort();
 79  
   }
 80  
 
 81  
   @Override
 82  
   public int readUnsignedShort() throws IOException {
 83  0
     return buffer.readUnsignedShort();
 84  
   }
 85  
 
 86  
   @Override
 87  
   public char readChar() throws IOException {
 88  0
     return buffer.readChar();
 89  
   }
 90  
 
 91  
   @Override
 92  
   public int readInt() throws IOException {
 93  0
     return buffer.readInt();
 94  
   }
 95  
 
 96  
   @Override
 97  
   public long readLong() throws IOException {
 98  0
     return buffer.readLong();
 99  
   }
 100  
 
 101  
   @Override
 102  
   public float readFloat() throws IOException {
 103  0
     return buffer.readFloat();
 104  
   }
 105  
 
 106  
   @Override
 107  
   public double readDouble() throws IOException {
 108  0
     return buffer.readDouble();
 109  
   }
 110  
 
 111  
   @Override
 112  
   public String readLine() throws IOException {
 113  
     // Note that this code is mostly copied from DataInputStream
 114  0
     char[] buf = new char[128];
 115  
 
 116  0
     int room = buf.length;
 117  0
     int offset = 0;
 118  
     int c;
 119  
 
 120  
   loop:
 121  
     while (true) {
 122  0
       c = buffer.readByte();
 123  0
       switch (c) {
 124  
       case -1:
 125  
       case '\n':
 126  0
         break loop;
 127  
       case '\r':
 128  0
         int c2 = buffer.readByte();
 129  0
         if ((c2 != '\n') && (c2 != -1)) {
 130  0
           buffer.readerIndex(buffer.readerIndex() - 1);
 131  
         }
 132  
         break loop;
 133  
       default:
 134  0
         if (--room < 0) {
 135  0
           char[] replacebuf = new char[offset + 128];
 136  0
           room = replacebuf.length - offset - 1;
 137  0
           System.arraycopy(buf, 0, replacebuf, 0, offset);
 138  0
           buf = replacebuf;
 139  
         }
 140  0
         buf[offset++] = (char) c;
 141  0
         break;
 142  
       }
 143  
     }
 144  0
     if ((c == -1) && (offset == 0)) {
 145  0
       return null;
 146  
     }
 147  0
     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  0
     int utflen = buffer.readUnsignedShort();
 154  
 
 155  0
     byte[] bytearr = new byte[utflen];
 156  0
     char[] chararr = new char[utflen];
 157  
 
 158  
     int c;
 159  
     int char2;
 160  
     int char3;
 161  0
     int count = 0;
 162  0
     int chararrCount = 0;
 163  
 
 164  0
     buffer.readBytes(bytearr, 0, utflen);
 165  
 
 166  0
     while (count < utflen) {
 167  0
       c = (int) bytearr[count] & 0xff;
 168  0
       if (c > 127) {
 169  0
         break;
 170  
       }
 171  0
       count++;
 172  0
       chararr[chararrCount++] = (char) c;
 173  
     }
 174  
 
 175  0
     while (count < utflen) {
 176  0
       c = (int) bytearr[count] & 0xff;
 177  0
       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  0
         count++;
 188  0
         chararr[chararrCount++] = (char) c;
 189  0
         break;
 190  
       case 12:
 191  
       case 13:
 192  
         /* 110x xxxx   10xx xxxx*/
 193  0
         count += 2;
 194  0
         if (count > utflen) {
 195  0
           throw new UTFDataFormatException(
 196  
               "malformed input: partial character at end");
 197  
         }
 198  0
         char2 = (int) bytearr[count - 1];
 199  0
         if ((char2 & 0xC0) != 0x80) {
 200  0
           throw new UTFDataFormatException(
 201  
                 "malformed input around byte " + count);
 202  
         }
 203  0
         chararr[chararrCount++] = (char) (((c & 0x1F) << 6) |
 204  
             (char2 & 0x3F));
 205  0
         break;
 206  
       case 14:
 207  
         /* 1110 xxxx  10xx xxxx  10xx xxxx */
 208  0
         count += 3;
 209  0
         if (count > utflen) {
 210  0
           throw new UTFDataFormatException(
 211  
               "malformed input: partial character at end");
 212  
         }
 213  0
         char2 = (int) bytearr[count - 2];
 214  0
         char3 = (int) bytearr[count - 1];
 215  0
         if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
 216  0
           throw new UTFDataFormatException(
 217  
               "malformed input around byte " + (count - 1));
 218  
         }
 219  0
         chararr[chararrCount++] = (char) (((c & 0x0F) << 12) |
 220  
             ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
 221  0
         break;
 222  
       default:
 223  
         /* 10xx xxxx,  1111 xxxx */
 224  0
         throw new UTFDataFormatException(
 225  
             "malformed input around byte " + count);
 226  
       }
 227  
     }
 228  
     // The number of chars produced may be less than utflen
 229  0
     return new String(chararr, 0, chararrCount);
 230  
   }
 231  
 }