/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { // Avoid overflowing the int too early by casting to a long. long newSize = Math.min(Integer.MAX_VALUE, (3L * size) / 2L); setCapacity((int) newSize); } this.size = size; }
@Override public long getEstimatedMemorySize() { JavaDataModel jdm = JavaDataModel.get(); long size = 0; size += jdm.object() + (key == null ? 0 : key.getCapacity()); size += jdm.object() + (val == null ? 0 : val.getCapacity()); return size; } }
private byte[] getValid(BytesWritable writable) { if(writable.getSize() == writable.getCapacity()) return writable.get(); else return ByteUtils.copy(writable.get(), 0, writable.getSize()); }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the capacity of the backing storage. * The data is preserved. * @param new_cap The new capacity in bytes. */ public void setCapacity(int new_cap) { if (new_cap != getCapacity()) { byte[] new_data = new byte[new_cap]; if (new_cap < size) { size = new_cap; } if (size != 0) { System.arraycopy(bytes, 0, new_data, 0, size); } bytes = new_data; } }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { setCapacity(size * 3 / 2); } this.size = size; }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { setCapacity(size * 3 / 2); } this.size = size; }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { setCapacity(size * 3 / 2); } this.size = size; }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { setCapacity(size * 3 / 2); } this.size = size; }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { setCapacity(size * 3 / 2); } this.size = size; }
/** * Change the size of the buffer. The values in the old range are preserved * and any new values are undefined. The capacity is changed if it is * necessary. * @param size The new number of bytes */ public void setSize(int size) { if (size > getCapacity()) { // Avoid overflowing the int too early by casting to a long. long newSize = Math.min(Integer.MAX_VALUE, (3L * size) / 2L); setCapacity((int) newSize); } this.size = size; }
public static byte[] getBytes(BytesWritable bw) { if (bw.getCapacity() == bw.getLength()) { return bw.getBytes(); } else { return copyBytes(bw); } }
@Override public void write(int b) throws IOException { if (target.getCapacity() < uncommittedLength + 1) { assert target.getCapacity() == uncommittedLength; int l = target.getLength(); target.setSize(uncommittedLength); target.setCapacity(uncommittedLength + 1); target.setSize(l); } target.getBytes()[uncommittedLength] = (byte) b; uncommittedLength++; } };
@Override public void write(int b) throws IOException { if (target.getCapacity() < uncommittedLength + 1) { assert target.getCapacity() == uncommittedLength; int l = target.getLength(); target.setSize(uncommittedLength); target.setCapacity(uncommittedLength + 1); target.setSize(l); } target.getBytes()[uncommittedLength] = (byte) b; uncommittedLength++; } };
public void testSizeChange() throws Exception { byte[] hadoop = "hadoop".getBytes(); BytesWritable buf = new BytesWritable(hadoop); int size = buf.getLength(); int orig_capacity = buf.getCapacity(); buf.setSize(size*2); int new_capacity = buf.getCapacity(); System.arraycopy(buf.getBytes(), 0, buf.getBytes(), size, size); assertTrue(new_capacity >= size * 2); assertEquals(size * 2, buf.getLength()); assertTrue(new_capacity != orig_capacity); buf.setSize(size*4); assertTrue(new_capacity != buf.getCapacity()); for(int i=0; i < size*2; ++i) { assertEquals(hadoop[i%size], buf.getBytes()[i]); } // ensure that copyBytes is exactly the right length assertEquals(size*4, buf.copyBytes().length); // shrink the buffer buf.setCapacity(1); // make sure the size has been cut down too assertEquals(1, buf.getLength()); // but that the data is still there assertEquals(hadoop[0], buf.getBytes()[0]); }