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);
}
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());
}
}
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;
}
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
}
}
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);
}
Aggregations