This project has retired. For details please refer to its Attic page.
ExtendedByteArrayDataOutput 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.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    public ExtendedByteArrayDataOutput(byte[] buf) {
44      if (buf == null) {
45        this.buf = new byte[DEFAULT_BYTES];
46      } else {
47        this.buf = buf;
48      }
49      dataOutput = new DataOutputStream(this);
50    }
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      this(buf);
60      this.count = pos;
61    }
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      this(DEFAULT_BYTES);
69    }
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    public ExtendedByteArrayDataOutput(int size) {
79      if (size < 0) {
80        throw new IllegalArgumentException("Negative initial size: " +
81            size);
82      }
83      buf = new byte[size];
84      dataOutput = new DataOutputStream(this);
85    }
86  
87    @Override
88    public void writeBoolean(boolean v) throws IOException {
89      dataOutput.writeBoolean(v);
90    }
91  
92    @Override
93    public void writeByte(int v) throws IOException {
94      dataOutput.writeByte(v);
95    }
96  
97    @Override
98    public void writeShort(int v) throws IOException {
99      dataOutput.writeShort(v);
100   }
101 
102   @Override
103   public void writeChar(int v) throws IOException {
104     dataOutput.writeChar(v);
105   }
106 
107   @Override
108   public void writeInt(int v) throws IOException {
109     dataOutput.writeInt(v);
110   }
111 
112   @Override
113   public void writeLong(long v) throws IOException {
114     dataOutput.writeLong(v);
115   }
116 
117   @Override
118   public void writeFloat(float v) throws IOException {
119     dataOutput.writeFloat(v);
120   }
121 
122   @Override
123   public void writeDouble(double v) throws IOException {
124     dataOutput.writeDouble(v);
125   }
126 
127   @Override
128   public void writeBytes(String s) throws IOException {
129     dataOutput.writeBytes(s);
130   }
131 
132   @Override
133   public void writeChars(String s) throws IOException {
134     dataOutput.writeChars(s);
135   }
136 
137   @Override
138   public void writeUTF(String s) throws IOException {
139     dataOutput.writeUTF(s);
140   }
141 
142   @Override
143   public void ensureWritable(int minSize) {
144     if ((count + minSize) > buf.length) {
145       buf = Arrays.copyOf(buf, Math.max(buf.length << 1, count + minSize));
146     }
147   }
148 
149   @Override
150   public void skipBytes(int bytesToSkip) {
151     ensureWritable(bytesToSkip);
152     count += bytesToSkip;
153   }
154 
155   @Override
156   public void writeInt(int position, int value) {
157     if (position + 4 > count) {
158       throw new IndexOutOfBoundsException(
159           "writeIntOnPosition: Tried to write int to position " + position +
160               " but current length is " + count);
161     }
162     buf[position] = (byte) ((value >>> 24) & 0xFF);
163     buf[position + 1] = (byte) ((value >>> 16) & 0xFF);
164     buf[position + 2] = (byte) ((value >>> 8) & 0xFF);
165     buf[position + 3] = (byte) ((value >>> 0) & 0xFF);
166   }
167 
168   @Override
169   public byte[] toByteArray(int offset, int length) {
170     if (offset + length > count) {
171       throw new IndexOutOfBoundsException(String.format("Offset: %d + " +
172           "Length: %d exceeds the size of buf : %d", offset, length, count));
173     }
174     return Arrays.copyOfRange(buf, offset, length);
175   }
176 
177   @Override
178   public byte[] getByteArray() {
179     return buf;
180   }
181 
182   @Override
183   public int getPos() {
184     return count;
185   }
186 }