use of java.io.ObjectOutputStream in project camel by apache.
the class AhcHelper method writeObjectToStream.
/**
* Writes the given object as response body to the output stream
*
* @param stream output stream
* @param target object to write
* @throws java.io.IOException is thrown if error writing
*/
public static void writeObjectToStream(OutputStream stream, Object target) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(stream);
oos.writeObject(target);
oos.flush();
IOHelper.close(oos);
}
use of java.io.ObjectOutputStream in project camel by apache.
the class HBaseHelper method toBytes.
public static byte[] toBytes(Object obj) {
if (obj instanceof byte[]) {
return (byte[]) obj;
} else if (obj instanceof Byte) {
return Bytes.toBytes((Byte) obj);
} else if (obj instanceof Short) {
return Bytes.toBytes((Short) obj);
} else if (obj instanceof Integer) {
return Bytes.toBytes((Integer) obj);
} else if (obj instanceof Long) {
return Bytes.toBytes((Long) obj);
} else if (obj instanceof Double) {
return Bytes.toBytes((Double) obj);
} else if (obj instanceof String) {
return Bytes.toBytes((String) obj);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
} catch (IOException e) {
LOG.warn("Error while serializing object. Null will be used.", e);
return null;
} finally {
IOHelper.close(oos);
IOHelper.close(baos);
}
}
}
use of java.io.ObjectOutputStream in project camel by apache.
the class HttpHelper method writeObjectToStream.
/**
* Writes the given object as response body to the output stream
*
* @param stream output stream
* @param target object to write
* @throws IOException is thrown if error writing
*/
public static void writeObjectToStream(OutputStream stream, Object target) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(stream);
oos.writeObject(target);
oos.flush();
IOHelper.close(oos);
}
use of java.io.ObjectOutputStream 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.ObjectOutputStream in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequestFailure.
/**
* Allocates a buffer and serializes the KvState request failure into it.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param requestId ID of the request responding to
* @param cause Failure cause
* @return Serialized KvState request failure message
* @throws IOException Serialization failures are forwarded
*/
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause) throws IOException {
ByteBuf buf = alloc.ioBuffer();
// Frame length is set at the end
buf.writeInt(0);
writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);
// Message
buf.writeLong(requestId);
try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
ObjectOutputStream out = new ObjectOutputStream(bbos)) {
out.writeObject(cause);
}
// Set frame length
int frameLength = buf.readableBytes() - 4;
buf.setInt(0, frameLength);
return buf;
}
Aggregations