Search in sources :

Example 86 with ObjectInputStream

use of java.io.ObjectInputStream in project flink by apache.

the class CommonTestUtils method createCopySerializable.

/**
	 * Creates a copy of an object via Java Serialization.
	 *
	 * @param original The original object.
	 * @return The copied object.
	 */
public static <T extends java.io.Serializable> T createCopySerializable(T original) throws IOException {
    if (original == null) {
        throw new IllegalArgumentException();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(original);
    oos.close();
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (ObjectInputStream ois = new ObjectInputStream(bais)) {
        @SuppressWarnings("unchecked") T copy = (T) ois.readObject();
        return copy;
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 87 with ObjectInputStream

use of java.io.ObjectInputStream in project flink by apache.

the class ContinuousFileReaderOperator method restoreState.

// ------------------------------------------------------------------------
//  Restoring / Migrating from an older Flink version.
// ------------------------------------------------------------------------
@Override
public void restoreState(FSDataInputStream in) throws Exception {
    LOG.info("{} (taskIdx={}) restoring state from an older Flink version.", getClass().getSimpleName(), getRuntimeContext().getIndexOfThisSubtask());
    // this is just to read the byte indicating if we have udf state or not
    int hasUdfState = in.read();
    Preconditions.checkArgument(hasUdfState == 0);
    final ObjectInputStream ois = new ObjectInputStream(in);
    final DataInputViewStreamWrapper div = new DataInputViewStreamWrapper(in);
    // read the split that was being read
    FileInputSplit currSplit = (FileInputSplit) ois.readObject();
    // read the pending splits list
    List<FileInputSplit> pendingSplits = new LinkedList<>();
    int noOfSplits = div.readInt();
    for (int i = 0; i < noOfSplits; i++) {
        FileInputSplit split = (FileInputSplit) ois.readObject();
        pendingSplits.add(split);
    }
    // read the state of the format
    Serializable formatState = (Serializable) ois.readObject();
    div.close();
    if (restoredReaderState == null) {
        restoredReaderState = new ArrayList<>();
    }
    // we do not know the modification time of the retrieved splits, so we assign them
    // artificial ones, with the only constraint that they respect the relative order of the
    // retrieved splits, because modification time is going to be used to sort the splits within
    // the "pending splits" priority queue.
    long now = getProcessingTimeService().getCurrentProcessingTime();
    long runningModTime = Math.max(now, noOfSplits + 1);
    TimestampedFileInputSplit currentSplit = createTimestampedFileSplit(currSplit, --runningModTime, formatState);
    restoredReaderState.add(currentSplit);
    for (FileInputSplit split : pendingSplits) {
        TimestampedFileInputSplit timestampedSplit = createTimestampedFileSplit(split, --runningModTime);
        restoredReaderState.add(timestampedSplit);
    }
    if (LOG.isDebugEnabled()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} (taskIdx={}) restored {} splits from legacy: {}.", getClass().getSimpleName(), getRuntimeContext().getIndexOfThisSubtask(), restoredReaderState.size(), restoredReaderState);
        }
    }
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) Serializable(java.io.Serializable) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) LinkedList(java.util.LinkedList) ObjectInputStream(java.io.ObjectInputStream)

Example 88 with ObjectInputStream

use of java.io.ObjectInputStream in project camel by apache.

the class JavaSpaceProducer method process.

public void process(Exchange exchange) throws Exception {
    Entry entry;
    Object body = exchange.getIn().getBody();
    if (!(body instanceof Entry)) {
        entry = new InEntry();
        if (body instanceof BeanInvocation) {
            ((InEntry) entry).correlationId = (new UID()).toString();
        }
        if (body instanceof byte[]) {
            ((InEntry) entry).binary = true;
            ((InEntry) entry).buffer = (byte[]) body;
        } else {
            ((InEntry) entry).binary = false;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(body);
            ((InEntry) entry).buffer = bos.toByteArray();
        }
    } else {
        entry = (Entry) body;
    }
    Transaction tnx = null;
    if (transactionHelper != null) {
        tnx = transactionHelper.getJiniTransaction(transactionTimeout).transaction;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Writing body : " + entry);
    }
    javaSpace.write(entry, tnx, Lease.FOREVER);
    if (ExchangeHelper.isOutCapable(exchange)) {
        OutEntry tmpl = new OutEntry();
        tmpl.correlationId = ((InEntry) entry).correlationId;
        OutEntry replyCamelEntry = null;
        while (replyCamelEntry == null) {
            replyCamelEntry = (OutEntry) javaSpace.take(tmpl, tnx, 100);
        }
        Object obj;
        if (replyCamelEntry.binary) {
            obj = replyCamelEntry.buffer;
        } else {
            ByteArrayInputStream bis = new ByteArrayInputStream(replyCamelEntry.buffer);
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
        }
        exchange.getOut().setBody(obj);
    }
    if (tnx != null) {
        tnx.commit();
    }
}
Also used : UID(java.rmi.server.UID) Entry(net.jini.core.entry.Entry) Transaction(net.jini.core.transaction.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) BeanInvocation(org.apache.camel.component.bean.BeanInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 89 with ObjectInputStream

use of java.io.ObjectInputStream in project camel by apache.

the class HttpHelper method deserializeJavaObjectFromStream.

/**
     * Deserializes the input stream to a Java object
     *
     * @param is input stream for the Java object
     * @param context the camel context which could help us to apply the customer classloader
     * @return the java object, or <tt>null</tt> if input stream was <tt>null</tt>
     * @throws ClassNotFoundException is thrown if class not found
     * @throws IOException can be thrown
     */
public static Object deserializeJavaObjectFromStream(InputStream is, CamelContext context) throws ClassNotFoundException, IOException {
    if (is == null) {
        return null;
    }
    Object answer = null;
    ObjectInputStream ois = new CamelObjectInputStream(is, context);
    try {
        answer = ois.readObject();
    } finally {
        IOHelper.close(ois);
    }
    return answer;
}
Also used : CamelObjectInputStream(org.apache.camel.util.CamelObjectInputStream) ObjectInputStream(java.io.ObjectInputStream) CamelObjectInputStream(org.apache.camel.util.CamelObjectInputStream)

Example 90 with ObjectInputStream

use of java.io.ObjectInputStream in project camel by apache.

the class AhcHelper method deserializeJavaObjectFromStream.

/**
     * Deserializes the input stream to a Java object
     *
     * @param is input stream for the Java object
     * @return the java object, or <tt>null</tt> if input stream was <tt>null</tt>
     * @throws ClassNotFoundException is thrown if class not found
     * @throws IOException can be thrown
     */
public static Object deserializeJavaObjectFromStream(InputStream is) throws ClassNotFoundException, IOException {
    if (is == null) {
        return null;
    }
    Object answer = null;
    ObjectInputStream ois = new ObjectInputStream(is);
    try {
        answer = ois.readObject();
    } finally {
        IOHelper.close(ois);
    }
    return answer;
}
Also used : ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20