Search in sources :

Example 1 with ByteArrayBuilder

use of org.spf4j.io.ByteArrayBuilder in project spf4j by zolyfarkas.

the class ThreadUsageSampler method getPeakThreadInfo.

@JmxExport
@SuppressFBWarnings({ "DM_DEFAULT_ENCODING", "NP_LOAD_OF_KNOWN_NULL_VALUE" })
public static String getPeakThreadInfo() {
    try (ByteArrayBuilder bab = new ByteArrayBuilder()) {
        PrintStream ps = new PrintStream(bab);
        writePeakThreadInfo(ps);
        return bab.toString(Charset.defaultCharset());
    }
}
Also used : PrintStream(java.io.PrintStream) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder) JmxExport(org.spf4j.jmx.JmxExport) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with ByteArrayBuilder

use of org.spf4j.io.ByteArrayBuilder in project spf4j by zolyfarkas.

the class Objects method clone.

@SuppressFBWarnings("OBJECT_DESERIALIZATION")
public static <T extends Serializable> T clone(final T t) {
    try (ByteArrayBuilder bos = new ByteArrayBuilder(256);
        ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(t);
        out.flush();
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.getBuffer(), 0, bos.size()))) {
            return (T) in.readObject();
        }
    } catch (IOException | ClassNotFoundException e) {
        throw new CloneFailedException("Failed to clone " + t, e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with ByteArrayBuilder

use of org.spf4j.io.ByteArrayBuilder in project spf4j by zolyfarkas.

the class DataFragment method writeTo.

public void writeTo(final RandomAccessFile raf) throws IOException {
    try (ByteArrayBuilder bos = new ByteArrayBuilder()) {
        DataOutput dos = new DataOutputStream(bos);
        writeTo(dos);
        raf.seek(location);
        raf.write(bos.getBuffer(), 0, bos.size());
    }
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder)

Example 4 with ByteArrayBuilder

use of org.spf4j.io.ByteArrayBuilder in project spf4j by zolyfarkas.

the class TSTable method writeTo.

void writeTo(final RandomAccessFile raf) throws IOException {
    try (ByteArrayBuilder bos = new ByteArrayBuilder()) {
        DataOutput dos = new DataOutputStream(bos);
        writeTo(dos);
        raf.seek(location);
        raf.write(bos.getBuffer(), 0, bos.size());
    }
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder)

Example 5 with ByteArrayBuilder

use of org.spf4j.io.ByteArrayBuilder in project spf4j by zolyfarkas.

the class AvroTest method testRw.

@Test
public void testRw() throws IOException {
    DataBlock data = DataBlock.newBuilder().setBaseTimestamp(0).setValues(Collections.EMPTY_LIST).build();
    try (ByteArrayBuilder bab = new ByteArrayBuilder()) {
        Schema schema = data.getSchema();
        SpecificDatumWriter<DataBlock> writer = new SpecificDatumWriter<>(schema);
        final BinaryEncoder directBinaryEncoder = EncoderFactory.get().directBinaryEncoder(bab, null);
        writer.write(data, directBinaryEncoder);
        directBinaryEncoder.flush();
        ByteArrayInputStream bis = new ByteArrayInputStream(bab.getBuffer(), 0, bab.size());
        SpecificDatumReader<DataBlock> reader = new SpecificDatumReader<>(schema);
        BinaryDecoder directBinaryDecoder = DecoderFactory.get().directBinaryDecoder(bis, null);
        DataBlock read = reader.read(null, directBinaryDecoder);
        Assert.assertEquals(read, data);
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(org.apache.avro.Schema) ByteArrayBuilder(org.spf4j.io.ByteArrayBuilder) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DataBlock(org.spf4j.tsdb2.avro.DataBlock) BinaryDecoder(org.apache.avro.io.BinaryDecoder) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Test(org.junit.Test)

Aggregations

ByteArrayBuilder (org.spf4j.io.ByteArrayBuilder)5 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataOutput (java.io.DataOutput)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 PrintStream (java.io.PrintStream)1 Schema (org.apache.avro.Schema)1 BinaryDecoder (org.apache.avro.io.BinaryDecoder)1 BinaryEncoder (org.apache.avro.io.BinaryEncoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)1 Test (org.junit.Test)1 JmxExport (org.spf4j.jmx.JmxExport)1 DataBlock (org.spf4j.tsdb2.avro.DataBlock)1