Search in sources :

Example 1 with OptimizedObjectInputStream

use of org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream in project ignite by apache.

the class OptimizedObjectStreamSelfTest method marshalUnmarshal.

/**
     * Marshals and unmarshals object.
     *
     * @param obj Original object.
     * @return Object after marshalling and unmarshalling.
     * @throws Exception In case of error.
     */
private <T> T marshalUnmarshal(@Nullable Object obj) throws Exception {
    OptimizedObjectOutputStream out = null;
    OptimizedObjectInputStream in = null;
    try {
        out = OptimizedObjectStreamRegistry.out();
        out.context(clsMap, CTX, null, true);
        out.writeObject(obj);
        byte[] arr = out.out().array();
        in = OptimizedObjectStreamRegistry.in();
        in.context(clsMap, CTX, null, getClass().getClassLoader());
        in.in().bytes(arr, arr.length);
        Object obj0 = in.readObject();
        checkHandles(out, in);
        return (T) obj0;
    } finally {
        OptimizedObjectStreamRegistry.closeOut(out);
        OptimizedObjectStreamRegistry.closeIn(in);
    }
}
Also used : OptimizedObjectInputStream(org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream) OptimizedObjectOutputStream(org.apache.ignite.internal.marshaller.optimized.OptimizedObjectOutputStream)

Example 2 with OptimizedObjectInputStream

use of org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream in project ignite by apache.

the class OptimizedObjectStreamSelfTest method testReadLine.

/**
     * @throws Exception If failed.
     */
public void testReadLine() throws Exception {
    OptimizedObjectInputStream in = new OptimizedObjectInputStream(new GridUnsafeDataInput());
    byte[] bytes = "line1\nline2\r\nli\rne3\nline4".getBytes();
    in.in().bytes(bytes, bytes.length);
    assertEquals("line1", in.readLine());
    assertEquals("line2", in.readLine());
    assertEquals("line3", in.readLine());
    assertEquals("line4", in.readLine());
}
Also used : OptimizedObjectInputStream(org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream) GridUnsafeDataInput(org.apache.ignite.internal.util.io.GridUnsafeDataInput)

Example 3 with OptimizedObjectInputStream

use of org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream in project ignite by apache.

the class OptimizedObjectStreamSelfTest method testReadToArray.

/**
     * @throws Exception If failed.
     */
public void testReadToArray() throws Exception {
    OptimizedObjectInputStream in = OptimizedObjectStreamRegistry.in();
    try {
        byte[] arr = new byte[50];
        for (int i = 0; i < arr.length; i++) arr[i] = (byte) i;
        in.in().bytes(arr, arr.length);
        byte[] buf = new byte[10];
        assertEquals(10, in.read(buf));
        for (int i = 0; i < buf.length; i++) assertEquals(i, buf[i]);
        buf = new byte[30];
        assertEquals(20, in.read(buf, 0, 20));
        for (int i = 0; i < buf.length; i++) assertEquals(i < 20 ? 10 + i : 0, buf[i]);
        buf = new byte[30];
        assertEquals(10, in.read(buf, 10, 10));
        for (int i = 0; i < buf.length; i++) assertEquals(i >= 10 && i < 20 ? 30 + (i - 10) : 0, buf[i]);
        buf = new byte[20];
        assertEquals(10, in.read(buf));
        for (int i = 0; i < buf.length; i++) assertEquals(i < 10 ? 40 + i : 0, buf[i]);
    } finally {
        OptimizedObjectStreamRegistry.closeIn(in);
    }
}
Also used : OptimizedObjectInputStream(org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream)

Aggregations

OptimizedObjectInputStream (org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream)3 OptimizedObjectOutputStream (org.apache.ignite.internal.marshaller.optimized.OptimizedObjectOutputStream)1 GridUnsafeDataInput (org.apache.ignite.internal.util.io.GridUnsafeDataInput)1