Coverage Report - org.apache.giraph.utils.DynamicChannelBufferOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
DynamicChannelBufferOutputStream
0%
0/69
0%
0/28
1.824
 
 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.DataOutput;
 21  
 import java.io.IOException;
 22  
 import java.nio.ByteOrder;
 23  
 
 24  
 import io.netty.buffer.Unpooled;
 25  
 import io.netty.buffer.ByteBuf;
 26  
 
 27  
 /**
 28  
  * Special output stream that can grow as needed and dumps to a
 29  
  * DynamicChannelBuffer.
 30  
  */
 31  
 public class DynamicChannelBufferOutputStream implements DataOutput {
 32  
   /** Internal dynamic channel buffer */
 33  
   private ByteBuf buffer;
 34  
 
 35  
   /**
 36  
    * Constructor
 37  
    *
 38  
    * @param estimatedLength Estimated length of the buffer
 39  
    */
 40  0
   public DynamicChannelBufferOutputStream(int estimatedLength) {
 41  0
     buffer = Unpooled.unreleasableBuffer(Unpooled.buffer(estimatedLength))
 42  0
         .order(ByteOrder.LITTLE_ENDIAN);
 43  
     // -- TODO unresolved what are benefits of using releasable?
 44  
     // currently nit because it is just used in 1 test file
 45  0
   }
 46  
 
 47  
   /**
 48  
    * Constructor with the buffer to use
 49  
    *
 50  
    * @param buffer Buffer to be written to (cleared before use)
 51  
    */
 52  0
   public DynamicChannelBufferOutputStream(ByteBuf buffer) {
 53  0
     this.buffer = buffer;
 54  0
     buffer.clear();
 55  0
   }
 56  
 
 57  
   /**
 58  
    * Get the dynamic channel buffer
 59  
    *
 60  
    * @return dynamic channel buffer (not a copy)
 61  
    */
 62  
   public ByteBuf getDynamicChannelBuffer() {
 63  0
     return buffer;
 64  
   }
 65  
 
 66  
   @Override
 67  
   public void write(int b) throws IOException {
 68  0
     buffer.writeByte(b);
 69  0
   }
 70  
 
 71  
   @Override
 72  
   public void write(byte[] b) throws IOException {
 73  0
     buffer.writeBytes(b);
 74  0
   }
 75  
 
 76  
   @Override
 77  
   public void write(byte[] b, int off, int len) throws IOException {
 78  0
     buffer.writeBytes(b, off, len);
 79  0
   }
 80  
 
 81  
   @Override
 82  
   public void writeBoolean(boolean v) throws IOException {
 83  0
     buffer.writeByte(v ? 1 : 0);
 84  0
   }
 85  
 
 86  
   @Override
 87  
   public void writeByte(int v) throws IOException {
 88  0
     buffer.writeByte(v);
 89  0
   }
 90  
 
 91  
   @Override
 92  
   public void writeShort(int v) throws IOException {
 93  0
     buffer.writeShort(v);
 94  0
   }
 95  
 
 96  
   @Override
 97  
   public void writeChar(int v) throws IOException {
 98  0
     buffer.writeChar(v);
 99  0
   }
 100  
 
 101  
   @Override
 102  
   public void writeInt(int v) throws IOException {
 103  0
     buffer.writeInt(v);
 104  0
   }
 105  
 
 106  
   @Override
 107  
   public void writeLong(long v) throws IOException {
 108  0
     buffer.writeLong(v);
 109  0
   }
 110  
 
 111  
   @Override
 112  
   public void writeFloat(float v) throws IOException {
 113  0
     buffer.writeFloat(v);
 114  0
   }
 115  
 
 116  
   @Override
 117  
   public void writeDouble(double v) throws IOException {
 118  0
     buffer.writeDouble(v);
 119  0
   }
 120  
 
 121  
   @Override
 122  
   public void writeBytes(String s) throws IOException {
 123  
     // Note that this code is mostly copied from DataOutputStream
 124  0
     int len = s.length();
 125  0
     for (int i = 0; i < len; i++) {
 126  0
       buffer.writeByte((byte) s.charAt(i));
 127  
     }
 128  0
   }
 129  
 
 130  
   @Override
 131  
   public void writeChars(String s) throws IOException {
 132  
     // Note that this code is mostly copied from DataOutputStream
 133  0
     int len = s.length();
 134  0
     for (int i = 0; i < len; i++) {
 135  0
       int v = s.charAt(i);
 136  0
       buffer.writeByte((v >>> 8) & 0xFF);
 137  0
       buffer.writeByte((v >>> 0) & 0xFF);
 138  
     }
 139  0
   }
 140  
 
 141  
   @Override
 142  
   public void writeUTF(String s) throws IOException {
 143  
     // Note that this code is mostly copied from DataOutputStream
 144  0
     int strlen = s.length();
 145  0
     int utflen = 0;
 146  
     int c;
 147  
 
 148  
     /* use charAt instead of copying String to char array */
 149  0
     for (int i = 0; i < strlen; i++) {
 150  0
       c = s.charAt(i);
 151  0
       if ((c >= 0x0001) && (c <= 0x007F)) {
 152  0
         utflen++;
 153  0
       } else if (c > 0x07FF) {
 154  0
         utflen += 3;
 155  
       } else {
 156  0
         utflen += 2;
 157  
       }
 158  
     }
 159  
 
 160  0
     buffer.writeByte((byte) ((utflen >>> 8) & 0xFF));
 161  0
     buffer.writeByte((byte) ((utflen >>> 0) & 0xFF));
 162  
 
 163  0
     int i = 0;
 164  0
     for (i = 0; i < strlen; i++) {
 165  0
       c = s.charAt(i);
 166  0
       if (!((c >= 0x0001) && (c <= 0x007F))) {
 167  0
         break;
 168  
       }
 169  0
       buffer.writeByte((byte) c);
 170  
     }
 171  
 
 172  0
     for (; i < strlen; i++) {
 173  0
       c = s.charAt(i);
 174  0
       if ((c >= 0x0001) && (c <= 0x007F)) {
 175  0
         buffer.writeByte((byte) c);
 176  
 
 177  0
       } else if (c > 0x07FF) {
 178  0
         buffer.writeByte((byte) (0xE0 | ((c >> 12) & 0x0F)));
 179  0
         buffer.writeByte((byte) (0x80 | ((c >>  6) & 0x3F)));
 180  0
         buffer.writeByte((byte) (0x80 | ((c >>  0) & 0x3F)));
 181  
       } else {
 182  0
         buffer.writeByte((byte) (0xC0 | ((c >>  6) & 0x1F)));
 183  0
         buffer.writeByte((byte) (0x80 | ((c >>  0) & 0x3F)));
 184  
       }
 185  
     }
 186  0
   }
 187  
 }