Search in sources :

Example 76 with ByteArrayInputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Event(org.apache.flink.cep.Event) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 77 with ByteArrayInputStream

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 78 with ByteArrayInputStream

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++;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) DataInput(java.io.DataInput) DataOutput(java.io.DataOutput) FileStatus(org.apache.hadoop.fs.FileStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 79 with ByteArrayInputStream

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++;
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputView(org.apache.flink.core.memory.DataInputView) IOException(java.io.IOException) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) IOException(java.io.IOException)

Example 80 with ByteArrayInputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)6879 Test (org.junit.Test)2274 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1791 InputStream (java.io.InputStream)1531 IOException (java.io.IOException)1400 DataInputStream (java.io.DataInputStream)600 ObjectInputStream (java.io.ObjectInputStream)597 X509Certificate (java.security.cert.X509Certificate)397 CertificateFactory (java.security.cert.CertificateFactory)355 ObjectOutputStream (java.io.ObjectOutputStream)333 File (java.io.File)279 ArrayList (java.util.ArrayList)270 Certificate (java.security.cert.Certificate)234 HashMap (java.util.HashMap)212 DataOutputStream (java.io.DataOutputStream)200 FileInputStream (java.io.FileInputStream)182 InputStreamReader (java.io.InputStreamReader)180 Test (org.testng.annotations.Test)171 Document (org.w3c.dom.Document)143 Map (java.util.Map)138