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