/** test the getInt() method */ public void testGetInt() { // reading 4 byte data from a 5 byte buffer byte[] testdata = { (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02, }; assertEquals(0xFFFFFF01, LittleEndian.getInt(testdata)); assertEquals(0x02FFFFFF, LittleEndian.getInt(testdata, 1)); }
public int serialize(int offset, byte[] data) { int pos = 0; LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); LittleEndian.putShort(data, 4 + offset + pos, field_1_formatFlags); return getRecordSize(); }
/** test the putDouble methods */ public void testPutDouble() { byte[] received = new byte[LittleEndian.DOUBLE_SIZE + 1]; LittleEndian.putDouble(received, 0, _doubles[0]); assertTrue(compareByteArrays(received, _double_array, 0, LittleEndian.DOUBLE_SIZE)); LittleEndian.putDouble(received, 1, _doubles[1]); byte[] expected = new byte[LittleEndian.DOUBLE_SIZE + 1]; System.arraycopy( _double_array, LittleEndian.DOUBLE_SIZE, expected, 1, LittleEndian.DOUBLE_SIZE); assertTrue(compareByteArrays(received, expected, 1, LittleEndian.DOUBLE_SIZE)); }
/** test the putInt method */ public void testPutInt() { // writing 4 byte data to a 5 byte buffer byte[] expected = { (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02, }; byte[] received = new byte[LittleEndian.INT_SIZE + 1]; LittleEndian.putInt(received, 0, 0xFFFFFF01); assertTrue(compareByteArrays(received, expected, 0, LittleEndian.INT_SIZE)); LittleEndian.putInt(received, 1, 0x02FFFFFF); assertTrue(compareByteArrays(received, expected, 1, LittleEndian.INT_SIZE)); }
/** test the getDouble() method */ public void testGetDouble() { assertEquals(_doubles[0], LittleEndian.getDouble(_double_array, 0), 0.000001); assertEquals( _doubles[1], LittleEndian.getDouble(_double_array, LittleEndian.DOUBLE_SIZE), 0.000001); assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array, 0))); double nan = LittleEndian.getDouble(_nan_double_array, 0); byte[] data = new byte[8]; LittleEndian.putDouble(data, 0, nan); for (int i = 0; i < data.length; i++) { assertEquals(data[i], _nan_double_array[i]); } }
/** test the getShort() method */ public void testGetShort() { byte[] testdata = new byte[LittleEndian.SHORT_SIZE + 1]; testdata[0] = 0x01; testdata[1] = (byte) 0xFF; testdata[2] = 0x02; short expected[] = new short[2]; expected[0] = (short) 0xFF01; expected[1] = 0x02FF; assertEquals(expected[0], LittleEndian.getShort(testdata)); assertEquals(expected[1], LittleEndian.getShort(testdata, 1)); }
public int serialize(int offset, byte[] data) { int pos = 0; LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); LittleEndian.putShort(data, 4 + offset + pos, field_1_id); data[6 + offset + pos] = field_2_textLength; data[7 + offset + pos] = field_3_undocumented; StringUtil.putUnicodeLE(field_4_text, data, 8 + offset + pos); return getRecordSize(); }
public void testGetUShort() { byte[] testdata = { (byte) 0x01, (byte) 0xFF, (byte) 0x02, }; byte[] testdata2 = { (byte) 0x0D, (byte) 0x93, (byte) 0xFF, }; int expected0 = 0xFF01; int expected1 = 0x02FF; int expected2 = 0x930D; int expected3 = 0xFF93; assertEquals(expected0, LittleEndian.getUShort(testdata)); assertEquals(expected1, LittleEndian.getUShort(testdata, 1)); assertEquals(expected2, LittleEndian.getUShort(testdata2)); assertEquals(expected3, LittleEndian.getUShort(testdata2, 1)); byte[] testdata3 = new byte[LittleEndian.SHORT_SIZE + 1]; LittleEndian.putUShort(testdata3, 0, expected2); LittleEndian.putUShort(testdata3, 1, expected3); assertEquals(testdata3[0], 0x0D); assertEquals(testdata3[1], (byte) 0x93); assertEquals(testdata3[2], (byte) 0xFF); assertEquals(expected2, LittleEndian.getUShort(testdata3)); assertEquals(expected3, LittleEndian.getUShort(testdata3, 1)); }
public int serialize(int offset, byte[] data) { int pos = 0; LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); data[4 + offset + pos] = field_1_linkType; data[5 + offset + pos] = field_2_referenceType; LittleEndian.putShort(data, 6 + offset + pos, field_3_options); LittleEndian.putShort(data, 8 + offset + pos, field_4_indexNumberFmtRecord); pos += field_5_formulaOfLink.serializeField(pos + 10 + offset, data); return getRecordSize(); }
/** test the PutShort method */ public void testPutShort() { byte[] expected = new byte[LittleEndian.SHORT_SIZE + 1]; expected[0] = 0x01; expected[1] = (byte) 0xFF; expected[2] = 0x02; byte[] received = new byte[LittleEndian.SHORT_SIZE + 1]; short testdata[] = new short[2]; testdata[0] = (short) 0xFF01; testdata[1] = 0x02FF; LittleEndian.putShort(received, 0, testdata[0]); assertTrue(compareByteArrays(received, expected, 0, LittleEndian.SHORT_SIZE)); LittleEndian.putShort(received, 1, testdata[1]); assertTrue(compareByteArrays(received, expected, 1, LittleEndian.SHORT_SIZE)); }
/** test the getLong method */ public void testGetLong() { // reading 8 byte values from a 9 byte buffer byte[] testdata = { (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02, }; assertEquals(0xFFFFFFFFFFFFFF01L, LittleEndian.getLong(testdata, 0)); assertEquals(0x02FFFFFFFFFFFFFFL, LittleEndian.getLong(testdata, 1)); }
/** test the readLong method */ public void testReadLong() throws IOException { long expected_value = 0x0201020102010201L; InputStream stream = new ByteArrayInputStream(_good_array); int count = 0; while (stream.available() > 0) { long value = LittleEndian.readLong(stream); assertEquals(value, expected_value); count++; } assertEquals(count, _good_array.length / LittleEndianConsts.LONG_SIZE); stream = new ByteArrayInputStream(_bad_array); try { LittleEndian.readLong(stream); fail("Should have caught BufferUnderrunException"); } catch (BufferUnderrunException ignored) { // as expected } }
/** test the putLong method */ public void testPutLong() { // writing 8 byte values to a 9 byte buffer byte[] expected = { (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02, }; byte[] received = new byte[LittleEndian.LONG_SIZE + 1]; long testdata0 = 0xFFFFFFFFFFFFFF01L; long testdata1 = 0x02FFFFFFFFFFFFFFL; LittleEndian.putLong(received, 0, testdata0); assertTrue(compareByteArrays(received, expected, 0, LittleEndian.LONG_SIZE)); LittleEndian.putLong(received, 1, testdata1); assertTrue(compareByteArrays(received, expected, 1, LittleEndian.LONG_SIZE)); }
void writeTo(byte mainStream[], int offset, HWPFOutputStream tableStream) throws IOException { int length = _fields.length / 2; LittleEndian.putShort(mainStream, offset, (short) length); offset += 2; for (int x = 0; x < length; x++) { UnhandledDataStructure ds = (UnhandledDataStructure) _unknownMap.get(Integer.valueOf(x)); if (ds != null) { _fields[x * 2] = tableStream.getOffset(); LittleEndian.putInt(mainStream, offset, tableStream.getOffset()); offset += 4; byte buf[] = ds.getBuf(); tableStream.write(buf); _fields[x * 2 + 1] = buf.length; LittleEndian.putInt(mainStream, offset, buf.length); offset += 4; } else { LittleEndian.putInt(mainStream, offset, _fields[x * 2]); offset += 4; LittleEndian.putInt(mainStream, offset, _fields[x * 2 + 1]); offset += 4; } } }
public FIBFieldHandler( byte mainStream[], int startOffset, byte tableStream[], HashSet offsetList, boolean areKnown) { _unknownMap = new HashMap(); int numFields = LittleEndian.getShort(mainStream, startOffset); int offset = startOffset + 2; _fields = new int[numFields * 2]; for (int x = 0; x < numFields; x++) { int fieldOffset = x * 8 + offset; int dsOffset = LittleEndian.getInt(mainStream, fieldOffset); fieldOffset += 4; int dsSize = LittleEndian.getInt(mainStream, fieldOffset); if (offsetList.contains(Integer.valueOf(x)) ^ areKnown && dsSize > 0) if (dsOffset + dsSize > tableStream.length) { log.log( POILogger.WARN, (new StringBuilder()) .append("Unhandled data structure points to outside the buffer. offset = ") .append(dsOffset) .append(", length = ") .append(dsSize) .append(", buffer length = ") .append(tableStream.length) .toString()); } else { UnhandledDataStructure unhandled = new UnhandledDataStructure(tableStream, dsOffset, dsSize); _unknownMap.put(Integer.valueOf(x), unhandled); } _fields[x * 2] = dsOffset; _fields[x * 2 + 1] = dsSize; } }
public int serialize(int offset, byte[] data) { int pos = 0; LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); LittleEndian.putShort(data, 4 + offset + pos, field_1_xBasis); LittleEndian.putShort(data, 6 + offset + pos, field_2_yBasis); LittleEndian.putShort(data, 8 + offset + pos, field_3_heightBasis); LittleEndian.putShort(data, 10 + offset + pos, field_4_scale); LittleEndian.putShort(data, 12 + offset + pos, field_5_indexToFontTable); return getRecordSize(); }
public int serialize(int offset, byte[] data) { int pos = 0; LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); LittleEndian.putInt(data, 4 + offset + pos, field_1_x); LittleEndian.putInt(data, 8 + offset + pos, field_2_y); LittleEndian.putInt(data, 12 + offset + pos, field_3_width); LittleEndian.putInt(data, 16 + offset + pos, field_4_height); return getRecordSize(); }
public void testUnsignedShort() { assertEquals(0xffff, LittleEndian.getUShort(new byte[] {(byte) 0xff, (byte) 0xff}, 0)); }
/** * Read the options field from header and return instance part of it. * * @param data the byte array to read from * @param offset the offset to start reading from * @return value of instance part of options field */ protected static short readInstance(byte data[], int offset) { final short options = LittleEndian.getShort(data, offset); return fInstance.getShortValue(options); }
/** * Reads the 8 byte header information and populates the <code>options</code> and <code>recordId * </code> records. * * @param data the byte array to read from * @param offset the offset to start reading from * @return the number of bytes remaining in this record. This may include the children if this is * a container. */ protected int readHeader(byte[] data, int offset) { _options = LittleEndian.getShort(data, offset); _recordId = LittleEndian.getShort(data, offset + 2); int remainingBytes = LittleEndian.getInt(data, offset + 4); return remainingBytes; }
@Test public void testGetUShort() { Binary binary = BinaryFactory.wrap(new byte[] {0x01, 0x02, (byte) 0xC1, (byte) 0xC2}); assertEquals("Wrong value", 0x0201, LittleEndian.getUShort(binary, 0)); assertEquals("Wrong value", 0xC2C1, LittleEndian.getUShort(binary, 2)); }
public void testUnsignedByteToInt() { assertEquals(255, LittleEndian.ubyteToInt((byte) 255)); }