Search in sources :

Example 1 with Bytes

use of com.github.davidmoten.rx2.Bytes in project navajo by Dexels.

the class TestRx method testGzip.

@Test
public void testGzip() throws FileNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] original = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tml_with_binary.xml")).reduce(baos, (byteout, bytes) -> {
        try {
            byteout.write(bytes);
        } catch (Exception e) {
        }
        return byteout;
    }).blockingGet().toByteArray();
    ByteArrayOutputStream baos_compressed = new ByteArrayOutputStream();
    byte[] compressed = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tml_with_binary.xml")).compose(StreamCompress.gzip()).doOnError(e -> e.printStackTrace()).reduce(baos_compressed, (byteout, bytes) -> {
        byteout.write(bytes);
        return byteout;
    }).blockingGet().toByteArray();
    ByteArrayOutputStream baos_reinflate = new ByteArrayOutputStream();
    byte[] reflated = Flowable.<byte[]>just(compressed).compose(StreamCompress.gunzip()).reduce(baos_reinflate, (byteout, bytes) -> {
        try {
            System.err.println("Bytes decompressed: " + bytes.length);
            byteout.write(bytes);
        } catch (Exception e) {
        }
        return byteout;
    }).blockingGet().toByteArray();
    System.err.println("original: " + original.length);
    System.err.println("gzipped: " + compressed.length);
    System.err.println("reinflated: " + reflated.length);
    Assert.assertArrayEquals(original, reflated);
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 2 with Bytes

use of com.github.davidmoten.rx2.Bytes in project navajo by Dexels.

the class TestRx method testGzipBig.

// TODO, reinstate this test. randomfile has been excluded from the build (it's 10M), create some random generation, I'd like
// to remove the randomdile from git as well
@Test
@Ignore
public void testGzipBig() throws IOException {
    ByteArrayOutputStream baos_compressed = new ByteArrayOutputStream();
    AtomicInteger size = new AtomicInteger();
    byte[] compressed = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("randomfile")).doOnNext(e -> size.addAndGet(e.length)).compose(StreamCompress.gzip()).doOnError(e -> e.printStackTrace()).reduce(baos_compressed, (byteout, bytes) -> {
        byteout.write(bytes);
        return byteout;
    }).blockingGet().toByteArray();
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
    ByteArrayOutputStream baos_decompressed = new ByteArrayOutputStream();
    copyResource(baos_decompressed, gis);
    Assert.assertEquals(size.get(), baos_decompressed.size());
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) GZIPInputStream(java.util.zip.GZIPInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Bytes

use of com.github.davidmoten.rx2.Bytes in project navajo by Dexels.

the class TestRx method repro.

@Test
public void repro() throws IOException {
    File compressed = File.createTempFile("dump", ".xml.deflated");
    File uncompressed = File.createTempFile("dump", ".xml");
    InputStream resourceAsStream = TestRx.class.getClassLoader().getResourceAsStream("tml_with_binary.xml");
    Bytes.from(resourceAsStream, 8192).doOnSubscribe(e -> System.err.println("Ataaaa")).doOnNext(b -> System.err.println("Bytes: " + b.length)).subscribe(StreamDocument.dumpToFile(uncompressed.getAbsolutePath()));
    System.err.println("Uncompressed file at: " + uncompressed.getAbsolutePath());
    Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("tml_with_binary.xml"), 8192).compose(StreamCompress.deflate()).subscribe(StreamDocument.dumpToFile(compressed.getAbsolutePath()));
    System.err.println("Compressed file at: " + compressed.getAbsolutePath());
    // 1,560
    Assert.assertTrue(uncompressed.exists());
    System.err.println("Compressed: " + compressed.length());
    System.err.println("Uncompressed: " + uncompressed.length());
    Assert.assertTrue(uncompressed.length() == 5258);
    Assert.assertTrue(compressed.length() < uncompressed.length());
// compressed.delete();
// uncompressed.delete();
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) Test(org.junit.Test)

Example 4 with Bytes

use of com.github.davidmoten.rx2.Bytes in project navajo by Dexels.

the class TestRx method testGzipOnly.

@Test
public void testGzipOnly() throws IOException {
    ByteArrayOutputStream baos_compressed = new ByteArrayOutputStream();
    byte[] compressed = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("TestBinaries.class")).compose(StreamCompress.gzip()).doOnError(e -> e.printStackTrace()).reduce(baos_compressed, (byteout, bytes) -> {
        byteout.write(bytes);
        return byteout;
    }).blockingGet().toByteArray();
    System.err.println("Compressed: " + compressed.length);
    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
    ByteArrayOutputStream baos_decompressed = new ByteArrayOutputStream();
    copyResource(baos_decompressed, gis);
    Assert.assertArrayEquals(Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("TestBinaries.class")).blockingFirst(), baos_decompressed.toByteArray());
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with Bytes

use of com.github.davidmoten.rx2.Bytes in project navajo by Dexels.

the class TestRx method testDeflate.

@Test
public void testDeflate() throws FileNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] original = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("TestBinaries.class")).reduce(baos, (byteout, bytes) -> {
        try {
            byteout.write(bytes);
        } catch (Exception e) {
        }
        return byteout;
    }).blockingGet().toByteArray();
    ByteArrayOutputStream baos_compressed = new ByteArrayOutputStream();
    byte[] compressed = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("TestBinaries.class")).compose(StreamCompress.deflate()).doOnError(e -> e.printStackTrace()).reduce(baos_compressed, (byteout, bytes) -> {
        byteout.write(bytes);
        return byteout;
    }).blockingGet().toByteArray();
    ByteArrayOutputStream baos_reinflate = new ByteArrayOutputStream();
    byte[] reflated = Flowable.<byte[]>just(compressed).compose(StreamCompress.inflate()).reduce(baos_reinflate, (byteout, bytes) -> {
        try {
            System.err.println("Bytes decompressed: " + bytes.length);
            byteout.write(bytes);
        } catch (Exception e) {
        }
        return byteout;
    }).blockingGet().toByteArray();
    System.err.println("original: " + original.length);
    System.err.println("deflated: " + compressed.length);
    System.err.println("reinflated: " + reflated.length);
    Assert.assertArrayEquals(original, reflated);
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamCompress(com.dexels.navajo.document.stream.StreamCompress) BufferedOutputStream(java.io.BufferedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XML(com.dexels.navajo.document.stream.xml.XML) Observable(io.reactivex.Observable) Subscriber(org.reactivestreams.Subscriber) OutputStream(java.io.OutputStream) XMLEvent(com.dexels.navajo.document.stream.xml.XMLEvent) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) Assert(org.junit.Assert) Bytes(com.github.davidmoten.rx2.Bytes) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

StreamCompress (com.dexels.navajo.document.stream.StreamCompress)5 StreamDocument (com.dexels.navajo.document.stream.StreamDocument)5 XML (com.dexels.navajo.document.stream.xml.XML)5 XMLEvent (com.dexels.navajo.document.stream.xml.XMLEvent)5 Bytes (com.github.davidmoten.rx2.Bytes)5 Flowable (io.reactivex.Flowable)5 Observable (io.reactivex.Observable)5 Disposable (io.reactivex.disposables.Disposable)5 BufferedInputStream (java.io.BufferedInputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 Arrays (java.util.Arrays)5 List (java.util.List)5 TimeUnit (java.util.concurrent.TimeUnit)5