it.unimi.dsi.fastutil.io.FastByteArrayInputStream Java Examples

The following examples show how to use it.unimi.dsi.fastutil.io.FastByteArrayInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MutableStringTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testSkipSelfDelimUTF8() throws IOException {
	final FastByteArrayOutputStream fastByteArrayOutputStream = new FastByteArrayOutputStream();
	new MutableString( "a" ).writeSelfDelimUTF8( fastByteArrayOutputStream );
	new MutableString( "b" ).writeSelfDelimUTF8( fastByteArrayOutputStream );
	new MutableString( "\u221E" ).writeSelfDelimUTF8( fastByteArrayOutputStream );
	new MutableString( "c" ).writeSelfDelimUTF8( fastByteArrayOutputStream );
	fastByteArrayOutputStream.flush();
	final FastByteArrayInputStream fastByteArrayInputStream = new FastByteArrayInputStream( fastByteArrayOutputStream.array );
	assertEquals( "a", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
	assertEquals( "b", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
	assertEquals( 1, MutableString.skipSelfDelimUTF8( fastByteArrayInputStream ) );
	assertEquals( "c", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
	fastByteArrayInputStream.position( 0 );
	assertEquals( "a", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
	assertEquals( 1, MutableString.skipSelfDelimUTF8( fastByteArrayInputStream ) );
	assertEquals( "\uu221E", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
	assertEquals( "c", new MutableString().readSelfDelimUTF8( fastByteArrayInputStream ).toString() );
}
 
Example #2
Source File: ObjectDiskQueue.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
/** Dequeues an object from the queue in FIFO fashion. */
@SuppressWarnings("unchecked")
public synchronized T dequeue() throws IOException {
	final int length = byteDiskQueue.dequeueInt();
	fbaos.array = ByteArrays.grow(fbaos.array, length);
	byteDiskQueue.dequeue(fbaos.array, 0, length);
	size--;
	try {
		return (T)BinIO.loadObject(new FastByteArrayInputStream(fbaos.array, 0, length));
	}
	catch (final ClassNotFoundException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
}
 
Example #3
Source File: ByteSerializerDeserializerTest.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
@Test
public void testINTEGER() throws IOException {
	Random r = new Random(0);
	for (int i = 0; i < 100; i++) {
		int x = r.nextInt();
		FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
		ByteSerializerDeserializer.INTEGER.toStream(Integer.valueOf(x), fbaos);
		Integer result = ByteSerializerDeserializer.INTEGER.fromStream(new FastByteArrayInputStream(fbaos.array));
		assertEquals(x, result.intValue());
	}
}
 
Example #4
Source File: Binary.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static EntityMetadata readMetadata(byte[] payload) {
    BinaryStream stream = new BinaryStream();
    stream.setBuffer(payload);
    long count = stream.getUnsignedVarInt();
    EntityMetadata m = new EntityMetadata();
    for (int i = 0; i < count; i++) {
        int key = (int) stream.getUnsignedVarInt();
        int type = (int) stream.getUnsignedVarInt();
        EntityData value = null;
        switch (type) {
            case Entity.DATA_TYPE_BYTE:
                value = new ByteEntityData(key, stream.getByte());
                break;
            case Entity.DATA_TYPE_SHORT:
                value = new ShortEntityData(key, stream.getLShort());
                break;
            case Entity.DATA_TYPE_INT:
                value = new IntEntityData(key, stream.getVarInt());
                break;
            case Entity.DATA_TYPE_FLOAT:
                value = new FloatEntityData(key, stream.getLFloat());
                break;
            case Entity.DATA_TYPE_STRING:
                value = new StringEntityData(key, stream.getString());
                break;
            case Entity.DATA_TYPE_NBT:
                int offset = stream.getOffset();
                FastByteArrayInputStream fbais = new FastByteArrayInputStream(stream.get());
                try {
                    CompoundTag tag = NBTIO.read(fbais, ByteOrder.LITTLE_ENDIAN, true);
                    value = new NBTEntityData(key, tag);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                stream.setOffset(offset + (int) fbais.position());
                break;
            case Entity.DATA_TYPE_POS:
                BlockVector3 v3 = stream.getSignedBlockPosition();
                value = new IntPositionEntityData(key, v3.x, v3.y, v3.z);
                break;
            case Entity.DATA_TYPE_LONG:
                value = new LongEntityData(key, stream.getVarLong());
                break;
            case Entity.DATA_TYPE_VECTOR3F:
                value = new Vector3fEntityData(key, stream.getVector3f());
                break;
        }
        if (value != null) m.put(value);
    }
    return m;
}
 
Example #5
Source File: ParallelBufferedWarcWriterTest.java    From BUbiNG with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecord() throws IOException, InterruptedException, URISyntaxException {

	for(boolean gzip: new boolean[] { true, false }) {
		final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
		final ParallelBufferedWarcWriter warcParallelOutputStream = new ParallelBufferedWarcWriter(out, gzip);
		final Thread thread[] = new Thread[NUM_THREADS];

		final URI fakeUri = new URI("http://this.is/a/fake");
		final RandomTestMocks.HttpResponse[] response = new RandomTestMocks.HttpResponse[NUM_RECORDS];
		for(int i = 0; i < NUM_THREADS; i++)
			(thread[i] = new Thread(Integer.toString(i)) {
				@Override
				public void run() {
					final int index = Integer.parseInt(getName());
					for (int i = index * (NUM_RECORDS / NUM_THREADS); i < (index + 1) * (NUM_RECORDS / NUM_THREADS); i++) {
						try {
							response[i] = new RandomTestMocks.HttpResponse(MAX_NUMBER_OF_HEADERS, MAX_LENGTH_OF_HEADER, MAX_LENGTH_OF_BODY, i);
							HttpResponseWarcRecord record = new HttpResponseWarcRecord(fakeUri, response[i]);
							warcParallelOutputStream.write(record);
							LOGGER.info("Thread " + index + " wrote record " + i);
						} catch(Exception e) { throw new RuntimeException(e); }
					}
				}
			}).start();


		for(Thread t: thread) t.join();
		warcParallelOutputStream.close();
		out.close();

		final FastBufferedInputStream in = new FastBufferedInputStream(new FastByteArrayInputStream(out.array, 0, out.length));
		WarcReader reader = gzip ? new CompressedWarcReader(in) : new UncompressedWarcReader(in);

		final boolean found[] = new boolean[NUM_RECORDS];
		for (int i = 0; i < NUM_RECORDS; i++) {
			final HttpResponseWarcRecord r = (HttpResponseWarcRecord) reader.read();
			final int pos = Integer.parseInt(r.getFirstHeader("Position").getValue());
			found[pos] = true;
			assertArrayEquals(ByteStreams.toByteArray(response[pos].getEntity().getContent()), ByteStreams.toByteArray(r.getEntity().getContent()));
		}
		in.close();

		for(int i = NUM_RECORDS; i-- != 0;) assertTrue(Integer.toString(i), found[i]);
	}
}
 
Example #6
Source File: BinaryStream.java    From Nemisys with GNU General Public License v3.0 4 votes vote down vote up
public Item getSlot() {
    int id = this.getVarInt();

    if (id <= 0) {
        return new Item(0, 0, 0);
    }
    int auxValue = this.getVarInt();
    int data = auxValue >> 8;
    if (data == Short.MAX_VALUE) {
        data = -1;
    }
    int cnt = auxValue & 0xff;

    int nbtLen = this.getLShort();
    byte[] nbt = new byte[0];
    if (nbtLen < Short.MAX_VALUE) {
        nbt = this.get(nbtLen);
    } else if (nbtLen == 65535) {
        int nbtTagCount = (int) getUnsignedVarInt();
        int offset = getOffset();
        FastByteArrayInputStream stream = new FastByteArrayInputStream(get());
        for (int i = 0; i < nbtTagCount; i++) {
            try {
                // TODO: 05/02/2019 This hack is necessary because we keep the raw NBT tag. Try to remove it.
                CompoundTag tag = NBTIO.read(stream, ByteOrder.LITTLE_ENDIAN, true);
                nbt = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        setOffset(offset + (int) stream.position());
    }

    //TODO
    int canPlaceOn = this.getVarInt();
    if (canPlaceOn > 0) {
        for (int i = 0; i < canPlaceOn; ++i) {
            this.getString();
        }
    }

    //TODO
    int canDestroy = this.getVarInt();
    if (canDestroy > 0) {
        for (int i = 0; i < canDestroy; ++i) {
            this.getString();
        }
    }

    return new Item(
            id, data, cnt, nbt
    );
}
 
Example #7
Source File: HttpData.java    From armeria with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new {@link InputStream} that is sourced from this data.
 */
default InputStream toInputStream() {
    return new FastByteArrayInputStream(array());
}