Coverage Report - org.apache.giraph.utils.ExtendedByteArrayDataOutput
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtendedByteArrayDataOutput
0%
0/58
0%
0/10
1.381
 
 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.ByteArrayOutputStream;
 21  
 import java.io.DataOutput;
 22  
 import java.io.DataOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.util.Arrays;
 25  
 
 26  
 /**
 27  
  * Adds some functionality to ByteArrayOutputStream,
 28  
  * such as an option to write int value over previously written data
 29  
  * and directly get the byte array.
 30  
  */
 31  
 public class ExtendedByteArrayDataOutput extends ByteArrayOutputStream
 32  
     implements ExtendedDataOutput {
 33  
   /** Default number of bytes */
 34  
   private static final int DEFAULT_BYTES = 32;
 35  
   /** Internal data output */
 36  
   private final DataOutput dataOutput;
 37  
 
 38  
   /**
 39  
    * Uses the byte array provided or if null, use a default size
 40  
    *
 41  
    * @param buf Buffer to use
 42  
    */
 43  0
   public ExtendedByteArrayDataOutput(byte[] buf) {
 44  0
     if (buf == null) {
 45  0
       this.buf = new byte[DEFAULT_BYTES];
 46  
     } else {
 47  0
       this.buf = buf;
 48  
     }
 49  0
     dataOutput = new DataOutputStream(this);
 50  0
   }
 51  
 
 52  
   /**
 53  
    * Uses the byte array provided at the given pos
 54  
    *
 55  
    * @param buf Buffer to use
 56  
    * @param pos Position in the buffer to start writing from
 57  
    */
 58  
   public ExtendedByteArrayDataOutput(byte[] buf, int pos) {
 59  0
     this(buf);
 60  0
     this.count = pos;
 61  0
   }
 62  
 
 63  
   /**
 64  
    * Creates a new byte array output stream. The buffer capacity is
 65  
    * initially 32 bytes, though its size increases if necessary.
 66  
    */
 67  
   public ExtendedByteArrayDataOutput() {
 68  0
     this(DEFAULT_BYTES);
 69  0
   }
 70  
 
 71  
   /**
 72  
    * Creates a new byte array output stream, with a buffer capacity of
 73  
    * the specified size, in bytes.
 74  
    *
 75  
    * @param size the initial size.
 76  
    * @exception  IllegalArgumentException if size is negative.
 77  
    */
 78  0
   public ExtendedByteArrayDataOutput(int size) {
 79  0
     if (size < 0) {
 80  0
       throw new IllegalArgumentException("Negative initial size: " +
 81  
           size);
 82  
     }
 83  0
     buf = new byte[size];
 84  0
     dataOutput = new DataOutputStream(this);
 85  0
   }
 86  
 
 87  
   @Override
 88  
   public void writeBoolean(boolean v) throws IOException {
 89  0
     dataOutput.writeBoolean(v);
 90  0
   }
 91  
 
 92  
   @Override
 93  
   public void writeByte(int v) throws IOException {
 94  0
     dataOutput.writeByte(v);
 95  0
   }
 96  
 
 97  
   @Override
 98  
   public void writeShort(int v) throws IOException {
 99  0
     dataOutput.writeShort(v);
 100  0
   }
 101  
 
 102  
   @Override
 103  
   public void writeChar(int v) throws IOException {
 104  0
     dataOutput.writeChar(v);
 105  0
   }
 106  
 
 107  
   @Override
 108  
   public void writeInt(int v) throws IOException {
 109  0
     dataOutput.writeInt(v);
 110  0
   }
 111  
 
 112  
   @Override
 113  
   public void writeLong(long v) throws IOException {
 114  0
     dataOutput.writeLong(v);
 115  0
   }
 116  
 
 117  
   @Override
 118  
   public void writeFloat(float v) throws IOException {
 119  0
     dataOutput.writeFloat(v);
 120  0
   }
 121  
 
 122  
   @Override
 123  
   public void writeDouble(double v) throws IOException {
 124  0
     dataOutput.writeDouble(v);
 125  0
   }
 126  
 
 127  
   @Override
 128  
   public void writeBytes(String s) throws IOException {
 129  0
     dataOutput.writeBytes(s);
 130  0
   }
 131  
 
 132  
   @Override
 133  
   public void writeChars(String s) throws IOException {
 134  0
     dataOutput.writeChars(s);
 135  0
   }
 136  
 
 137  
   @Override
 138  
   public void writeUTF(String s) throws IOException {
 139  0
     dataOutput.writeUTF(s);
 140  0
   }
 141  
 
 142  
   @Override
 143  
   public void ensureWritable(int minSize) {
 144  0
     if ((count + minSize) > buf.length) {
 145  0
       buf = Arrays.copyOf(buf, Math.max(buf.length << 1, count + minSize));
 146  
     }
 147  0
   }
 148  
 
 149  
   @Override
 150  
   public void skipBytes(int bytesToSkip) {
 151  0
     ensureWritable(bytesToSkip);
 152  0
     count += bytesToSkip;
 153  0
   }
 154  
 
 155  
   @Override
 156  
   public void writeInt(int position, int value) {
 157  0
     if (position + 4 > count) {
 158  0
       throw new IndexOutOfBoundsException(
 159  
           "writeIntOnPosition: Tried to write int to position " + position +
 160  
               " but current length is " + count);
 161  
     }
 162  0
     buf[position] = (byte) ((value >>> 24) & 0xFF);
 163  0
     buf[position + 1] = (byte) ((value >>> 16) & 0xFF);
 164  0
     buf[position + 2] = (byte) ((value >>> 8) & 0xFF);
 165  0
     buf[position + 3] = (byte) ((value >>> 0) & 0xFF);
 166  0
   }
 167  
 
 168  
   @Override
 169  
   public byte[] toByteArray(int offset, int length) {
 170  0
     if (offset + length > count) {
 171  0
       throw new IndexOutOfBoundsException(String.format("Offset: %d + " +
 172  0
           "Length: %d exceeds the size of buf : %d", offset, length, count));
 173  
     }
 174  0
     return Arrays.copyOfRange(buf, offset, length);
 175  
   }
 176  
 
 177  
   @Override
 178  
   public byte[] getByteArray() {
 179  0
     return buf;
 180  
   }
 181  
 
 182  
   @Override
 183  
   public int getPos() {
 184  0
     return count;
 185  
   }
 186  
 }