Search in sources :

Example 1 with ObjectOutputStream

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);
}
Also used : ObjectOutputStream(java.io.ObjectOutputStream)

Example 2 with ObjectOutputStream

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

Example 3 with ObjectOutputStream

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);
}
Also used : ObjectOutputStream(java.io.ObjectOutputStream)

Example 4 with ObjectOutputStream

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();
    }
}
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 5 with ObjectOutputStream

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;
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)958 ByteArrayOutputStream (java.io.ByteArrayOutputStream)657 ObjectInputStream (java.io.ObjectInputStream)386 ByteArrayInputStream (java.io.ByteArrayInputStream)332 IOException (java.io.IOException)312 FileOutputStream (java.io.FileOutputStream)132 Test (org.junit.Test)130 File (java.io.File)75 BufferedOutputStream (java.io.BufferedOutputStream)46 ObjectOutput (java.io.ObjectOutput)35 OutputStream (java.io.OutputStream)35 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)24 ArrayList (java.util.ArrayList)24 InputStream (java.io.InputStream)22 FileNotFoundException (java.io.FileNotFoundException)20 Serializable (java.io.Serializable)15 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14 Map (java.util.Map)13