use of java.io.ByteArrayInputStream in project flink by apache.
the class SharedBufferTest method testSharedBufferSerialization.
@Test
public void testSharedBufferSerialization() throws IOException, ClassNotFoundException {
SharedBuffer<String, Event> sharedBuffer = new SharedBuffer<>(Event.createTypeSerializer());
int numberEvents = 8;
Event[] events = new Event[numberEvents];
final long timestamp = 1L;
for (int i = 0; i < numberEvents; i++) {
events[i] = new Event(i + 1, "e" + (i + 1), i);
}
sharedBuffer.put("a1", events[0], timestamp, null, null, 0, DeweyNumber.fromString("1"));
sharedBuffer.put("a[]", events[1], timestamp, "a1", events[0], timestamp, DeweyNumber.fromString("1.0"));
sharedBuffer.put("a1", events[2], timestamp, null, null, 0, DeweyNumber.fromString("2"));
sharedBuffer.put("a[]", events[2], timestamp, "a[]", events[1], timestamp, DeweyNumber.fromString("1.0"));
sharedBuffer.put("a[]", events[3], timestamp, "a[]", events[2], timestamp, DeweyNumber.fromString("1.0"));
sharedBuffer.put("a[]", events[3], timestamp, "a1", events[2], timestamp, DeweyNumber.fromString("2.0"));
sharedBuffer.put("a[]", events[4], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("1.0"));
sharedBuffer.put("a[]", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.1"));
sharedBuffer.put("b", events[5], timestamp, "a[]", events[3], timestamp, DeweyNumber.fromString("2.0.0"));
sharedBuffer.put("b", events[5], timestamp, "a[]", events[4], timestamp, DeweyNumber.fromString("1.0.0"));
sharedBuffer.put("a[]", events[6], timestamp, "a[]", events[5], timestamp, DeweyNumber.fromString("1.1"));
sharedBuffer.put("b", events[7], timestamp, "a[]", events[6], timestamp, DeweyNumber.fromString("1.1.0"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(sharedBuffer);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
SharedBuffer<String, Event> copy = (SharedBuffer<String, Event>) ois.readObject();
assertEquals(sharedBuffer, copy);
}
use of java.io.ByteArrayInputStream in project flink by apache.
the class CollectionInputFormatTest method testDeserializationFailure.
@Test
public void testDeserializationFailure() {
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer)) {
// a mock serializer that fails when writing
CollectionInputFormat<ElementType> inFormat = new CollectionInputFormat<ElementType>(Collections.singleton(new ElementType()), new TestSerializer(true, false));
out.writeObject(inFormat);
out.close();
ByteArrayInputStream bais = new ByteArrayInputStream(buffer.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
try {
in.readObject();
fail("should throw an exception");
} catch (Exception e) {
assertTrue(e.getCause() instanceof TestException);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.io.ByteArrayInputStream in project hadoop by apache.
the class TestFileStatus method testFileStatusWritable.
/**
* Check that the write and readField methods work correctly.
*/
@Test
public void testFileStatusWritable() throws Exception {
FileStatus[] tests = { new FileStatus(1, false, 5, 3, 4, 5, null, "", "", new Path("/a/b")), new FileStatus(0, false, 1, 2, 3, new Path("/")), new FileStatus(1, false, 5, 3, 4, 5, null, "", "", new Path("/a/b")) };
LOG.info("Writing FileStatuses to a ByteArrayOutputStream");
// Writing input list to ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutput out = new DataOutputStream(baos);
for (FileStatus fs : tests) {
fs.write(out);
}
LOG.info("Creating ByteArrayInputStream object");
DataInput in = new DataInputStream(new ByteArrayInputStream(baos.toByteArray()));
LOG.info("Testing if read objects are equal to written ones");
FileStatus dest = new FileStatus();
int iterator = 0;
for (FileStatus fs : tests) {
dest.readFields(in);
assertEquals("Different FileStatuses in iteration " + iterator, dest, fs);
iterator++;
}
}
use of java.io.ByteArrayInputStream in project flink by apache.
the class FromElementsFunction method run.
@Override
public void run(SourceContext<T> ctx) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(elementsSerialized);
final DataInputView input = new DataInputViewStreamWrapper(bais);
// if we are restored from a checkpoint and need to skip elements, skip them now.
int toSkip = numElementsToSkip;
if (toSkip > 0) {
try {
while (toSkip > 0) {
serializer.deserialize(input);
toSkip--;
}
} catch (Exception e) {
throw new IOException("Failed to deserialize an element from the source. " + "If you are using user-defined serialization (Value and Writable types), check the " + "serialization functions.\nSerializer is " + serializer);
}
this.numElementsEmitted = this.numElementsToSkip;
}
final Object lock = ctx.getCheckpointLock();
while (isRunning && numElementsEmitted < numElements) {
T next;
try {
next = serializer.deserialize(input);
} catch (Exception e) {
throw new IOException("Failed to deserialize an element from the source. " + "If you are using user-defined serialization (Value and Writable types), check the " + "serialization functions.\nSerializer is " + serializer);
}
synchronized (lock) {
ctx.collect(next);
numElementsEmitted++;
}
}
}
use of java.io.ByteArrayInputStream in project flink by apache.
the class FoldApplyAllWindowFunction method open.
@Override
public void open(Configuration configuration) throws Exception {
super.open(configuration);
if (accSerializer == null) {
throw new RuntimeException("No serializer set for the fold accumulator type. " + "Probably the setOutputType method was not called.");
}
if (serializedInitialValue == null) {
throw new RuntimeException("No initial value was serialized for the fold " + "window function. Probably the setOutputType method was not called.");
}
ByteArrayInputStream bais = new ByteArrayInputStream(serializedInitialValue);
DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais);
initialValue = accSerializer.deserialize(in);
}
Aggregations