/** * Read an integer of this type from the given input stream */ public int read(DataInputStream in) throws IOException { switch (this) { case INT8: return in.readByte(); case INT16: return in.readShort(); case INT32: return in.readInt(); default: throw new RuntimeException("Unknown itype: " + this); } }
byte[] test = new byte[] {0, 0, 1, 0, 0, 0, 1, 1, 8, 9}; DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test)); int value0 = stream.readInt(); int value1 = stream.readInt(); byte value2 = stream.readByte(); byte value3 = stream.readByte(); stream.close(); System.out.println(value0 + " " + value1 + " " + value2 + " " + value3);
protected long parseSize (final DataInputStream din, final byte type, final boolean useIntOnError, final long defaultValue) throws IOException { if (type == 'i') return (long)readUChar(din); if (type == 'I') return (long)readUShort(din); if (type == 'l') return (long)readUInt(din); if (type == 'L') return din.readLong(); if (useIntOnError) { long result = (long)((short)type & 0xFF) << 24; result |= (long)((short)din.readByte() & 0xFF) << 16; result |= (long)((short)din.readByte() & 0xFF) << 8; result |= (long)((short)din.readByte() & 0xFF); return result; } return defaultValue; }
/** * Deserialize a bloom filter * Read a byte stream, which was written by {@linkplain #serialize(OutputStream, BloomKFilter)} * into a {@code BloomKFilter} * * @param in input bytestream * * @return deserialized BloomKFilter */ public static BloomKFilter deserialize(InputStream in) throws IOException { if (in == null) { throw new IOException("Input stream is null"); } try { DataInputStream dataInputStream = new DataInputStream(in); int numHashFunc = dataInputStream.readByte(); int bitsetArrayLen = dataInputStream.readInt(); long[] data = new long[bitsetArrayLen]; for (int i = 0; i < bitsetArrayLen; i++) { data[i] = dataInputStream.readLong(); } return new BloomKFilter(data, numHashFunc); } catch (RuntimeException e) { IOException io = new IOException("Unable to deserialize BloomKFilter"); io.initCause(e); throw io; } }
music=new byte[(int) file.length()];//size & length of the file InputStream is = new FileInputStream (file); BufferedInputStream bis = new BufferedInputStream (is, 8000); DataInputStream dis = new DataInputStream (bis); // Create a DataInputStream to read the audio data from the saved file int i = 0; // Read the file into the "music" array while (dis.available() > 0) { music[i] = dis.readByte(); // This assignment does not reverse the order i++; } dis.close(); // Close the input stream
private void readResourceTypeToken() throws IOException { int resourceTypeId = dataIn.readInt(); String resourceTypeName = dataIn.readUTF(); String resourceTypeDesc = dataIn.readUTF(); int statCount = dataIn.readUnsignedShort(); while (resourceTypeId >= resourceTypeTable.length) { String statName = dataIn.readUTF(); byte typeCode = dataIn.readByte(); boolean isCounter = dataIn.readBoolean(); boolean largerBetter = isCounter; // default
@Test public void testSafeWithOnFailedConsumer() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(30); final DataOutputStream dos = new DataOutputStream(baos); LongConsumer consumer = LongConsumer.Util.safe(new UnsafeConsumer(dos), new LongConsumer() { @Override public void accept(long value) { baos.write(0); } }); consumer.accept(10L); consumer.accept(20L); consumer.accept(-5L); consumer.accept(-8L); consumer.accept(500L); final byte[] result = baos.toByteArray(); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(result)); assertThat(dis.readLong(), is(10L)); assertThat(dis.readLong(), is(20L)); assertThat(dis.readByte(), is((byte) 0)); assertThat(dis.readByte(), is((byte) 0)); assertThat(dis.readLong(), is(500L)); }
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer)); byte opCode = inputStream.readByte(); inputStream.readUTF();
DataInputStream dis=null; try { dis=new DataInputStream(input); version=dis.readShort(); byte flags=dis.readByte();
/** Now parse the old Writable format. It was a list of Map entries. Each map entry was a key and a value of * a byte []. The old map format had a byte before each entry that held a code which was short for the key or * value type. We know it was a byte [] so in below we just read and dump it. * @throws IOException */ void parseWritable(final DataInputStream in) throws IOException { // First clear the map. Otherwise we will just accumulate entries every time this method is called. this.map.clear(); // Read the number of entries in the map int entries = in.readInt(); // Then read each key/value pair for (int i = 0; i < entries; i++) { byte [] key = Bytes.readByteArray(in); // We used to read a byte that encoded the class type. Read and ignore it because it is always byte [] in hfile in.readByte(); byte [] value = Bytes.readByteArray(in); this.map.put(key, value); } }
private boolean readHandshakeForReceiver(DataInputStream dis) { try { byte b = dis.readByte(); if (b != 0) { throw new IllegalStateException( b)); byte handshakeByte = dis.readByte(); if (handshakeByte != HANDSHAKE_VERSION) { throw new IllegalStateException( this.sharedResource = dis.readBoolean(); this.preserveOrder = dis.readBoolean(); this.uniqueId = dis.readLong(); if (this.remoteVersion == null || (this.remoteVersion.compareTo(Version.GFE_80) >= 0)) { dominoNumber = dis.readInt(); if (this.sharedResource) { dominoNumber = 0;
/** * Reads a class descriptor from the source stream. * * @return the class descriptor read from the source stream. * @throws ClassNotFoundException * if a class for one of the objects cannot be found. * @throws IOException * if an error occurs while reading from the source stream. */ protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass newClassDesc = new ObjectStreamClass(); String name = input.readUTF(); if (name.length() == 0) { throw new IOException("The stream is corrupted"); } newClassDesc.setName(name); newClassDesc.setSerialVersionUID(input.readLong()); newClassDesc.setFlags(input.readByte()); /* * We must register the class descriptor before reading field * descriptors. If called outside of readObject, the descriptorHandle * might be unset. */ if (descriptorHandle == -1) { descriptorHandle = nextHandle(); } registerObjectRead(newClassDesc, descriptorHandle, false); readFieldDescriptors(newClassDesc); return newClassDesc; }
private long readCompactValue() throws IOException { long v = dataIn.readByte(); if (v < MIN_1BYTE_COMPACT_VALUE) { if (v == COMPACT_VALUE_2_TOKEN) { v = dataIn.readShort(); } else { int bytesToRead = ((byte) v - COMPACT_VALUE_2_TOKEN) + 2; v = dataIn.readByte(); // note the first byte will be a signed byte. bytesToRead--; while (bytesToRead > 0) { v <<= 8; v |= dataIn.readUnsignedByte(); bytesToRead--; } } } return v; }
int dataLength = -1; try { DataInputStream din = new DataInputStream(in); strategyOrdinal = din.readByte(); numHashFunctions = UnsignedBytes.toInt(din.readByte()); dataLength = din.readInt(); data[i] = din.readLong();
@Override public Schema readSchema(InputStream in) throws IOException { DataInputStream dataInputStream = new DataInputStream(in); if (dataInputStream.readByte() != 0) { throw new IOException("Unknown data format. Magic number does not match"); } else { int schemaId = dataInputStream.readInt(); try { return schemaRegistryClient.getById(schemaId); } catch (RestClientException e) { throw new IOException(format("Could not find schema with id %s in registry", schemaId), e); } } }
@Override public void create () { FileHandle handle = Gdx.files.internal("data/arial.ttf"); bytes = new byte[(int)handle.length()]; DataInputStream in = new DataInputStream(handle.read()); for (int i = 0; i < 100; i++) { try { bytes[i] = in.readByte(); } catch (IOException e) { e.printStackTrace(); } } }
DataInputStream dis = new DataInputStream(new FileInputStream(outputFile)); for (int i = 0; i < 3; i++) { long size = dis.readLong(); Assert.assertEquals(size, records[i].length + 1); for (int j = 0; j < size - 1; j++) { Assert.assertEquals(dis.readByte(), records[i][j]); Assert.assertEquals(dis.readByte(), '\n');