Search in sources :

Example 96 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class CompositeBufferViewTests method testGetReader.

/**
 * Tests {@link CompositeBufferView#getReader()}.
 */
@Override
@Test
public void testGetReader() throws IOException {
    val components = createComponents();
    val cb = BufferView.wrap(components);
    val expectedSize = components.stream().mapToInt(BufferView::getLength).sum();
    val expected = new SequenceInputStream(Iterators.asEnumeration(components.stream().map(BufferView::getReader).iterator()));
    val actual = cb.getReader();
    AssertExtensions.assertStreamEquals("", expected, actual, expectedSize);
}
Also used : lombok.val(lombok.val) SequenceInputStream(java.io.SequenceInputStream) Test(org.junit.Test)

Example 97 with SequenceInputStream

use of java.io.SequenceInputStream in project pravega by pravega.

the class CompositeBufferViewTests method testSlice.

/**
 * Tests {@link CompositeBufferView#slice(int, int)} and {@link CompositeBufferView#getReader(int, int)}.
 */
@Test
@Override
public void testSlice() throws IOException {
    val components = createComponents();
    val cb = BufferView.wrap(components);
    val expectedSize = components.stream().mapToInt(BufferView::getLength).sum();
    val expected = StreamHelpers.readAll(new SequenceInputStream(Iterators.asEnumeration(components.stream().map(BufferView::getReader).iterator())), expectedSize);
    val expectedInitialAllocatedSize = components.stream().mapToInt(BufferView::getAllocatedLength).sum();
    Assert.assertEquals("Unexpected initial allocated size.", expectedInitialAllocatedSize, cb.getAllocatedLength());
    val componentIndexByOffset = new TreeMap<Integer, Integer>();
    int offset = 0;
    for (int i = 0; i < components.size(); i++) {
        val c = components.get(i);
        componentIndexByOffset.put(offset, i);
        offset += c.getLength();
    }
    for (int i = 0; i < expectedSize / 2; i++) {
        int sliceLength = expectedSize - 2 * i;
        val slice = cb.slice(i, sliceLength);
        val sliceData = slice.getCopy();
        AssertExtensions.assertArrayEquals("slice(offset, length)", expected, i, sliceData, 0, sliceLength);
        val sliceReader = cb.getReader(i, sliceLength);
        val sliceReaderData = StreamHelpers.readAll(sliceReader, sliceLength);
        AssertExtensions.assertArrayEquals("getReader(offset, length)", expected, i, sliceReaderData, 0, sliceLength);
        val startComponentIndex = componentIndexByOffset.floorEntry(i).getValue();
        val endComponentIndex = componentIndexByOffset.floorEntry(i + sliceLength - 1).getValue();
        val expectedAllocatedSize = IntStream.rangeClosed(startComponentIndex, endComponentIndex).mapToObj(components::get).mapToInt(BufferView::getAllocatedLength).sum();
        Assert.assertEquals("Unexpected allocated size for slice " + i + "-" + (i + sliceLength), expectedAllocatedSize, slice.getAllocatedLength());
    }
}
Also used : lombok.val(lombok.val) SequenceInputStream(java.io.SequenceInputStream) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 98 with SequenceInputStream

use of java.io.SequenceInputStream in project hive by apache.

the class BeeLine method initializeConsoleReader.

public ConsoleReader initializeConsoleReader(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        // ### NOTE: fix for sf.net bug 879425.
        // Working around an issue in jline-2.1.2, see https://github.com/jline/jline/issues/10
        // by appending a newline to the end of inputstream
        InputStream inputStreamAppendedNewline = new SequenceInputStream(inputStream, new ByteArrayInputStream((new String("\n")).getBytes()));
        consoleReader = new ConsoleReader(inputStreamAppendedNewline, getErrorStream());
        // jline will detect if <tab> is regular character
        consoleReader.setCopyPasteDetection(true);
    } else {
        consoleReader = new ConsoleReader(getInputStream(), getErrorStream());
    }
    // disable the expandEvents for the purpose of backward compatibility
    consoleReader.setExpandEvents(false);
    try {
        // now set the output for the history
        if (this.history != null) {
            consoleReader.setHistory(this.history);
        } else {
            consoleReader.setHistoryEnabled(false);
        }
    } catch (Exception e) {
        handleException(e);
    }
    if (inputStream instanceof FileInputStream || inputStream instanceof FSDataInputStream) {
        // from script.. no need to load history and no need of completer, either
        return consoleReader;
    }
    consoleReader.addCompleter(new BeeLineCompleter(this));
    return consoleReader;
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ConsoleReader(jline.console.ConsoleReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) TTransportException(org.apache.thrift.transport.TTransportException) BeelineConfFileParseException(org.apache.hive.beeline.hs2connection.BeelineConfFileParseException) BeelineSiteParseException(org.apache.hive.beeline.hs2connection.BeelineSiteParseException) EOFException(java.io.EOFException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseException(org.apache.commons.cli.ParseException) JdbcUriParseException(org.apache.hive.jdbc.JdbcUriParseException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 99 with SequenceInputStream

use of java.io.SequenceInputStream in project j2objc by google.

the class OldSequenceInputStreamTest method test_read$BII_Excpetion.

public void test_read$BII_Excpetion() throws IOException {
    byte[] buf = new byte[4];
    si.read(buf, 0, 2);
    si.read(buf, 2, 1);
    simple2.throwExceptionOnNextUse = true;
    si.read(buf, 3, 1);
    assertEquals("Wrong stuff read!", "Hell", new String(buf));
    simple1.throwExceptionOnNextUse = true;
    try {
        si.read(buf, 3, 1);
        fail("IOException not thrown!");
    } catch (IOException e) {
    // expected
    }
    buf = new byte[10];
    simple1 = new Support_ASimpleInputStream(s1);
    simple2 = new Support_ASimpleInputStream(s2);
    si = new SequenceInputStream(simple1, simple2);
    try {
        si.read(buf, -1, 1);
        fail("IndexOutOfBoundsException was not thrown");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        si.read(buf, 0, -1);
        fail("IndexOutOfBoundsException was not thrown");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
    try {
        si.read(buf, 1, 10);
        fail("IndexOutOfBoundsException was not thrown");
    } catch (IndexOutOfBoundsException e) {
    // Expected
    }
}
Also used : SequenceInputStream(java.io.SequenceInputStream) Support_ASimpleInputStream(tests.support.Support_ASimpleInputStream) IOException(java.io.IOException)

Example 100 with SequenceInputStream

use of java.io.SequenceInputStream in project JGroups by belaban.

the class FRAG4 method assembleMessage.

@Override
protected Message assembleMessage(Message[] fragments, boolean needs_deserialization, FragHeader hdr) throws Exception {
    if (fragments[0] instanceof FragmentedMessage) {
        if (Objects.equals(local_addr, fragments[0].getSrc()))
            return ((FragmentedMessage) fragments[0]).getOriginalMessage();
        InputStream seq = new SequenceInputStream(Util.enumerate(fragments, 0, fragments.length, m -> new ByteArrayDataInputStream(m.getArray(), m.getOffset(), m.getLength())));
        DataInput in = new DataInputStream(seq);
        Message retval = msg_factory.create(hdr.getOriginalType());
        retval.readFrom(in);
        return retval;
    }
    int combined_length = 0, index = 0;
    for (Message fragment : fragments) combined_length += fragment.getLength();
    byte[] combined_buffer = new byte[combined_length];
    // doesn't copy the payload, but copies the headers
    Message retval = fragments[0].copy(false, true);
    for (int i = 0; i < fragments.length; i++) {
        Message fragment = fragments[i];
        // help garbage collection a bit
        fragments[i] = null;
        byte[] tmp = fragment.getArray();
        int length = fragment.getLength(), offset = fragment.getOffset();
        System.arraycopy(tmp, offset, combined_buffer, index, length);
        index += length;
    }
    return retval.setArray(combined_buffer, 0, combined_buffer.length);
}
Also used : DataInputStream(java.io.DataInputStream) Util(org.jgroups.util.Util) FragmentedMessage(org.jgroups.FragmentedMessage) SequenceInputStream(java.io.SequenceInputStream) Range(org.jgroups.util.Range) Objects(java.util.Objects) BytesMessage(org.jgroups.BytesMessage) List(java.util.List) Message(org.jgroups.Message) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) DataInput(java.io.DataInput) Address(org.jgroups.Address) InputStream(java.io.InputStream) DataInput(java.io.DataInput) FragmentedMessage(org.jgroups.FragmentedMessage) SequenceInputStream(java.io.SequenceInputStream) FragmentedMessage(org.jgroups.FragmentedMessage) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) DataInputStream(java.io.DataInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) InputStream(java.io.InputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) DataInputStream(java.io.DataInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Aggregations

SequenceInputStream (java.io.SequenceInputStream)123 InputStream (java.io.InputStream)78 ByteArrayInputStream (java.io.ByteArrayInputStream)67 IOException (java.io.IOException)47 ArrayList (java.util.ArrayList)32 FileInputStream (java.io.FileInputStream)24 BufferedInputStream (java.io.BufferedInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Vector (java.util.Vector)10 Test (org.junit.Test)10 FileOutputStream (java.io.FileOutputStream)9 List (java.util.List)9 lombok.val (lombok.val)9 File (java.io.File)8 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 InputStreamReader (java.io.InputStreamReader)7 ByteBuffer (java.nio.ByteBuffer)6 Support_ASimpleInputStream (tests.support.Support_ASimpleInputStream)6 Reader (java.io.Reader)5