Search in sources :

Example 1 with MemorizingBufferedInputStream

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

the class Converter method load.

@Nullable
public static SampleNode load(@WillNotClose final InputStream fis) throws IOException {
    try (MemorizingBufferedInputStream bis = new MemorizingBufferedInputStream(fis)) {
        final PushbackInputStream pis = new PushbackInputStream(bis);
        final SpecificDatumReader<StackSampleElement> reader = new SpecificDatumReader<>(StackSampleElement.getClassSchema());
        final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(pis, null);
        return convert(new Iterator<StackSampleElement>() {

            @Override
            public boolean hasNext() {
                try {
                    int read = pis.read();
                    pis.unread(read);
                    return read >= 0;
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }

            @Override
            @SuppressFBWarnings
            public StackSampleElement next() {
                try {
                    return reader.read(null, decoder);
                } catch (IOException ex) {
                    NoSuchElementException e = new NoSuchElementException();
                    e.addSuppressed(ex);
                    throw e;
                }
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        });
    }
}
Also used : MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BinaryDecoder(org.apache.avro.io.BinaryDecoder) PushbackInputStream(java.io.PushbackInputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) StackSampleElement(org.spf4j.base.avro.StackSampleElement) NoSuchElementException(java.util.NoSuchElementException) Nullable(javax.annotation.Nullable)

Example 2 with MemorizingBufferedInputStream

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

the class Converter method load.

@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public static SampleNode load(final File file) throws IOException {
    try (MemorizingBufferedInputStream bis = new MemorizingBufferedInputStream(Files.newInputStream(file.toPath()))) {
        final PushbackInputStream pis = new PushbackInputStream(bis);
        final SpecificDatumReader<ASample> reader = new SpecificDatumReader<>(ASample.SCHEMA$);
        final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(pis, null);
        return convert(new Iterator<ASample>() {

            @Override
            public boolean hasNext() {
                try {
                    int read = pis.read();
                    pis.unread(read);
                    return read >= 0;
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }

            @Override
            @SuppressFBWarnings
            public ASample next() {
                try {
                    return reader.read(null, decoder);
                } catch (IOException ex) {
                    NoSuchElementException e = new NoSuchElementException();
                    e.addSuppressed(ex);
                    throw e;
                }
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        });
    }
}
Also used : MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ASample(org.spf4j.ssdump2.avro.ASample) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) BinaryDecoder(org.apache.avro.io.BinaryDecoder) PushbackInputStream(java.io.PushbackInputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) NoSuchElementException(java.util.NoSuchElementException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with MemorizingBufferedInputStream

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

the class TSDBReader method resetStream.

private void resetStream(final long position) throws IOException {
    byteChannel.position(position);
    bis = new CountingInputStream(new MemorizingBufferedInputStream(Channels.newInputStream(byteChannel), bufferSize), position);
    decoder = DecoderFactory.get().directBinaryDecoder(bis, decoder);
}
Also used : MemorizingBufferedInputStream(org.spf4j.io.MemorizingBufferedInputStream) CountingInputStream(org.spf4j.io.CountingInputStream)

Aggregations

MemorizingBufferedInputStream (org.spf4j.io.MemorizingBufferedInputStream)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 IOException (java.io.IOException)2 PushbackInputStream (java.io.PushbackInputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 NoSuchElementException (java.util.NoSuchElementException)2 BinaryDecoder (org.apache.avro.io.BinaryDecoder)2 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)2 Nullable (javax.annotation.Nullable)1 StackSampleElement (org.spf4j.base.avro.StackSampleElement)1 CountingInputStream (org.spf4j.io.CountingInputStream)1 ASample (org.spf4j.ssdump2.avro.ASample)1