Package org.auraframework.util

Examples of org.auraframework.util.LimitedLengthInputStream


        createTestStream(5);
        final StreamFinishedListener listener = new TestStreamFinishedListener();
        byte[] array = new byte[5];

        // Try it with a regular read
        InputStream in = new LimitedLengthInputStream(wrappedStream, 5, listener);
        try {
            assertEquals(5, in.available());
            for (int i = 0; i < 5; i++) {
                assertFalse(callbackCalled);
                assertEquals(i, in.read());
                assertEquals(4 - i, in.available());
            }
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read());
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array read
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, 5, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array+pos+length read (aligned with actual underlying
        // bytes)
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, 5, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array, 0, array.length));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array, 0, array.length));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array+pos+length read (not aligned with actual
        // underlying bytes)
        array = new byte[10];
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, 5, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array, 0, array.length));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array, 0, array.length));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with skip
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, 5, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.skip(5));
            assertTrue(callbackCalled);
            callbackCalled = false;
            assertEquals(0, in.skip(1));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }
    }
View Full Code Here


        createTestStream(5);
        final StreamFinishedListener listener = new TestStreamFinishedListener();
        byte[] array = new byte[5];

        // Close the stream, which should put us ahead three bytes
        InputStream in = new LimitedLengthInputStream(wrappedStream, 3, listener);
        in.close();

        // Ensure that none of the methods work
        assertEquals(-1, in.read());
        assertEquals(-1, in.read(array));
        assertEquals(-1, in.read(array, 0, array.length));
        assertEquals(0, in.available());
        assertEquals(0, in.skip(1));

        // Try close() again. Nothing should happen
        in.close();

        // Ensure that our wrapped stream has two bytes left in it
        assertEquals(3, wrappedStream.read());
        assertEquals(4, wrappedStream.read());
        assertEquals(-1, wrappedStream.read());
View Full Code Here

        assertEquals(4, wrappedStream.read());
        assertEquals(-1, wrappedStream.read());
    }

    public void testMarkingUnsupported() throws Exception {
        InputStream in = new LimitedLengthInputStream(createTestStream(10), 5, new TestStreamFinishedListener());
        try {
            // Nothing should happen
            in.mark(3);
            in.mark(0);
            in.mark(Integer.MIN_VALUE);
            in.mark(Integer.MAX_VALUE);

            // Ensure that mark is unsupported (apologies to anyone named Mark)
            assertFalse(in.markSupported());

            // Reset isn't supported
            try {
                in.reset();
                fail("reset should have failed");
            } catch (IOException e) {
                assertTrue(e.getMessage().contains("mark not supported"));
            }
        } finally {
            in.close();
        }
    }
View Full Code Here

        }
    }

    public void testBadConstruction() throws Exception {
        try {
            new LimitedLengthInputStream(null, 1, null);
            fail();
        } catch (IllegalArgumentException e) {
            // Expected
        }
        try {
            new LimitedLengthInputStream(new ByteArrayInputStream(new byte[] { 1 }), -1, null);
            fail();
        } catch (IllegalArgumentException e) {
            // Expected
        }
        try {
            new LimitedLengthInputStream(null, -1, null);
            fail();
        } catch (IllegalArgumentException e) {
            // Expected
        }
    }
View Full Code Here

    }

    public void testLimitOfZero() throws Exception {

        // We get the callback upon construction in this case
        LimitedLengthInputStream in = new LimitedLengthInputStream(createTestStream(10), 0,
                new TestStreamFinishedListener());
        try {
            assertEquals(0, in.getLength());
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read());
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(-1, in.read(new byte[5]));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(-1, in.read(new byte[5], 0, 5));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(0, in.skip(1));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }
    }
View Full Code Here

        createTestStream(10);
        final StreamFinishedListener listener = new TestStreamFinishedListener();
        final byte[] array = new byte[5];

        // Try it with a regular read
        LimitedLengthInputStream in = new LimitedLengthInputStream(wrappedStream, 1, listener);
        try {
            assertEquals(1, in.getLength());
            assertFalse(callbackCalled);
            assertEquals(1, in.available());
            assertEquals(0, in.read());
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read());
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array read
        in = new LimitedLengthInputStream(wrappedStream, 1, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(1, in.available());
            assertEquals(1, in.read(array));
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array+pos+length read
        in = new LimitedLengthInputStream(wrappedStream, 1, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(1, in.available());
            assertEquals(1, in.read(array, 0, array.length));
            assertEquals(2, array[0]);
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array, 0, array.length));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with skip
        in = new LimitedLengthInputStream(wrappedStream, 1, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(1, in.available());
            assertEquals(1, in.skip(1));
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(0, in.skip(1));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }
    }
View Full Code Here

        createTestStream(5);
        final StreamFinishedListener listener = new TestStreamFinishedListener();
        byte[] array = new byte[5];

        // Try it with a regular read
        LimitedLengthInputStream in = new LimitedLengthInputStream(wrappedStream, limit, listener);
        try {
            assertEquals(limit, in.getLength());
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            for (int i = 0; i < 5; i++) {
                assertEquals(i, in.read());
                assertFalse(callbackCalled);
                assertEquals(4 - i, in.available());
            }
            assertEquals(-1, in.read());
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read());
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(limit, in.getLength());
        } finally {
            in.close();
        }

        // Try it with the array read
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, limit, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(-1, in.read(array));
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array+pos+length read (aligned with actual underlying
        // bytes)
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, limit, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array, 0, array.length));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(-1, in.read(array, 0, array.length));
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array, 0, array.length));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with the array+pos+length read (not aligned with actual
        // underlying bytes)
        array = new byte[10];
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, limit, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.read(array, 0, array.length));
            for (int i = 0; i < 5; i++) {
                assertEquals(i, array[i]);
            }
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(-1, in.read(array, 0, array.length));
            assertTrue(callbackCalled);
            assertEquals(0, in.available());
            callbackCalled = false;
            assertEquals(-1, in.read(array, 0, array.length));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }

        // Try it with skip
        createTestStream(5);
        in = new LimitedLengthInputStream(wrappedStream, limit, listener);
        try {
            assertFalse(callbackCalled);
            assertEquals(5, in.available());
            assertEquals(5, in.skip(5));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
            assertEquals(0, in.skip(1));
            assertFalse(callbackCalled); // The contract of skip() doesn't let
                                         // us definitively say that we're at
                                         // the EOF, so the callback won't get
                                         // called until we read
            assertEquals(-1, in.read());
            assertEquals(0, in.available());
            assertTrue(callbackCalled);
            callbackCalled = false;
            assertEquals(0, in.skip(1));
            assertFalse(callbackCalled);
            assertEquals(0, in.available());
        } finally {
            in.close();
        }
    }
View Full Code Here

            current = readString(token, false);
            token = STRING;
            break;
        case BINARY_STREAM: {
            if (binaryInput != null) {
                current = new LimitedLengthInputStream(binaryInput, binaryInput.readLong(), BINARY_STREAM_FINISHED_LISTENER);
            } else {
                throw new JsonStreamParseException(
                        "Binary data encountered in a JsonStreamReader that was not constructed to support binary data");
            }
        }
View Full Code Here

TOP

Related Classes of org.auraframework.util.LimitedLengthInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.