Search in sources :

Example 96 with DataInput

use of java.io.DataInput in project geode by apache.

the class PdxSerializableJUnitTest method testByteFormatForLongStrings.

@Test
public void testByteFormatForLongStrings() throws Exception {
    boolean myFlag = true;
    short myShort = 25;
    String myString1 = "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1." + "A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.A very long string1.";
    long myLong = 15654;
    String myString2 = "Class4_myString2";
    String myString3 = "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. " + "Even longer string3. Even longer string3. Even longer string3. Even longer string3. Even longer string3. ";
    int myInt = 1420;
    float myFloat = 123.023f;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    SimpleClass1 pdx = new SimpleClass1(myFlag, myShort, myString1, myLong, myString2, myString3, myInt, myFloat);
    DataSerializer.writeObject(pdx, out);
    int typeId = getPdxTypeIdForClass(SimpleClass1.class);
    HeapDataOutputStream hdos1 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString1, hdos1);
    byte[] str1Bytes = hdos1.toByteArray();
    HeapDataOutputStream hdos2 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString2, hdos2);
    byte[] str2Bytes = hdos2.toByteArray();
    HeapDataOutputStream hdos3 = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeString(myString3, hdos3);
    byte[] str3Bytes = hdos3.toByteArray();
    int length = 1 + /* myFlag */
    2 + /* myShort */
    8 + /* myLong */
    4 + /* myInt */
    4 + /* myFloat */
    str1Bytes.length + str2Bytes.length + str3Bytes.length + (2 * 2);
    int offset1 = 1 + 2;
    int offset2 = offset1 + 8 + str1Bytes.length;
    int offset3 = offset1 + 8 + str1Bytes.length + str2Bytes.length;
    byte[] actual = out.toByteArray();
    int floatBytes = Float.floatToRawIntBits(myFloat);
    Byte[] expected = new Byte[] { // byte
    DSCODE.PDX, // int -
    (byte) (length >> 24), // int -
    (byte) (length >> 16), // int -
    (byte) (length >> 8), // int -
    (byte) length, // int -
    (byte) (typeId >> 24), // int -
    (byte) (typeId >> 16), // int -
    (byte) (typeId >> 8), // int -
    (byte) typeId, // boolean - myFlag = true
    1, // short - myShort
    (byte) (myShort >> 8), // short - myShort
    (byte) myShort, (byte) (myLong >> 56), (byte) (myLong >> 48), (byte) (myLong >> 40), (byte) (myLong >> 32), // long -
    (byte) (myLong >> 24), // long -
    (byte) (myLong >> 16), // long -
    (byte) (myLong >> 8), // long -
    (byte) myLong, // int -
    (byte) (myInt >> 24), // int -
    (byte) (myInt >> 16), // int -
    (byte) (myInt >> 8), // int -
    (byte) myInt, // myInt
    (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), // float - myFloat
    (byte) floatBytes, // offset of myString3
    (byte) (offset3 >> 8), // offset of myString3
    (byte) offset3, // offset of myString2
    (byte) (offset2 >> 8), // offset of myString2
    (byte) offset2 };
    for (int i = (str1Bytes.length - 1); i >= 0; i--) {
        expected = // +
        (Byte[]) ArrayUtils.insert(expected, offset1 + PdxWriterImpl.HEADER_SIZE, str1Bytes[i]);
    // 5
    // for:
    // 1
    // for
    // DSCODE.PDX
    // and
    // 4
    // for
    // byte
    // stream
    // length
    }
    for (int i = (str2Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset2 + PdxWriterImpl.HEADER_SIZE, str2Bytes[i]);
    }
    for (int i = (str3Bytes.length - 1); i >= 0; i--) {
        expected = (Byte[]) ArrayUtils.insert(expected, offset3 + PdxWriterImpl.HEADER_SIZE, str3Bytes[i]);
    }
    StringBuffer msg = new StringBuffer("Actual output: ");
    for (byte val : actual) {
        msg.append(val + ", ");
    }
    msg.append("\nExpected output: ");
    for (byte val : expected) {
        msg.append(val + ", ");
    }
    if (actual.length != expected.length) {
        System.out.println(msg.toString());
    }
    assertTrue("Mismatch in length, actual.length: " + actual.length + " and expected length: " + expected.length, actual.length == expected.length);
    for (int i = 0; i < actual.length; i++) {
        if (actual[i] != expected[i]) {
            System.out.println(msg.toString());
        }
        assertTrue("Mismatch at index " + i, actual[i] == expected[i]);
    }
    System.out.println("\n");
    DataInput in = new DataInputStream(new ByteArrayInputStream(actual));
    SimpleClass1 actualVal = (SimpleClass1) DataSerializer.readObject(in);
    // System.out.println("actualVal..."+actualVal);
    assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
    c.setReadSerialized(true);
    try {
        in = new DataInputStream(new ByteArrayInputStream(actual));
        PdxInstance pi = (PdxInstance) DataSerializer.readObject(in);
        actualVal = (SimpleClass1) pi.getObject();
        assertTrue("Mismatch in write and read value: Value Write..." + pdx + " Value Read..." + actualVal, pdx.equals(actualVal));
        assertTrue(pi.hasField("myFlag"));
        assertTrue(pi.hasField("myShort"));
        assertTrue(pi.hasField("myString1"));
        assertTrue(pi.hasField("myLong"));
        assertTrue(pi.hasField("myString2"));
        assertTrue(pi.hasField("myString3"));
        assertTrue(pi.hasField("myInt"));
        assertTrue(pi.hasField("myFloat"));
        assertEquals(pdx.isMyFlag(), pi.getField("myFlag"));
        assertEquals(pdx.getMyShort(), pi.getField("myShort"));
        assertEquals(pdx.getMyString1(), pi.getField("myString1"));
        assertEquals(pdx.getMyLong(), pi.getField("myLong"));
        assertEquals(pdx.getMyString2(), pi.getField("myString2"));
        assertEquals(pdx.getMyString3(), pi.getField("myString3"));
        assertEquals(pdx.getMyInt(), pi.getField("myInt"));
        assertEquals(pdx.getMyFloat(), pi.getField("myFloat"));
        PdxReaderImpl reader = (PdxReaderImpl) pi;
        PdxType type = reader.getPdxType();
        assertEquals(SimpleClass1.class.getName(), type.getClassName());
        assertEquals(8, type.getFieldCount());
        assertEquals(2, type.getVariableLengthFieldCount());
        assertEquals(0, type.getPdxField("myFlag").getFieldIndex());
        assertEquals(1, type.getPdxField("myShort").getFieldIndex());
        assertEquals(2, type.getPdxField("myString1").getFieldIndex());
        assertEquals(3, type.getPdxField("myLong").getFieldIndex());
        assertEquals(4, type.getPdxField("myString2").getFieldIndex());
        assertEquals(5, type.getPdxField("myString3").getFieldIndex());
        assertEquals(6, type.getPdxField("myInt").getFieldIndex());
        assertEquals(7, type.getPdxField("myFloat").getFieldIndex());
        assertEquals(FieldType.BOOLEAN, type.getPdxField("myFlag").getFieldType());
        assertEquals(FieldType.SHORT, type.getPdxField("myShort").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString1").getFieldType());
        assertEquals(FieldType.LONG, type.getPdxField("myLong").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString2").getFieldType());
        assertEquals(FieldType.STRING, type.getPdxField("myString3").getFieldType());
        assertEquals(FieldType.INT, type.getPdxField("myInt").getFieldType());
        assertEquals(FieldType.FLOAT, type.getPdxField("myFloat").getFieldType());
        assertEquals("myFlag", type.getPdxField("myFlag").getFieldName());
        assertEquals("myShort", type.getPdxField("myShort").getFieldName());
        assertEquals("myString1", type.getPdxField("myString1").getFieldName());
        assertEquals("myLong", type.getPdxField("myLong").getFieldName());
        assertEquals("myString2", type.getPdxField("myString2").getFieldName());
        assertEquals("myString3", type.getPdxField("myString3").getFieldName());
        assertEquals("myInt", type.getPdxField("myInt").getFieldName());
        assertEquals("myFloat", type.getPdxField("myFloat").getFieldName());
        assertEquals(0, type.getPdxField("myFlag").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myShort").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myString1").getVarLenFieldSeqId());
        assertEquals(0, type.getPdxField("myLong").getVarLenFieldSeqId());
        assertEquals(1, type.getPdxField("myString2").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myString3").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myInt").getVarLenFieldSeqId());
        assertEquals(2, type.getPdxField("myFloat").getVarLenFieldSeqId());
        assertEquals(false, type.getPdxField("myFlag").isVariableLengthType());
        assertEquals(false, type.getPdxField("myShort").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString1").isVariableLengthType());
        assertEquals(false, type.getPdxField("myLong").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString2").isVariableLengthType());
        assertEquals(true, type.getPdxField("myString3").isVariableLengthType());
        assertEquals(false, type.getPdxField("myInt").isVariableLengthType());
        assertEquals(false, type.getPdxField("myFloat").isVariableLengthType());
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.isMyFlag() ? 1 : 0) }), reader.getRaw(0));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyShort() >> 8), (byte) pdx.getMyShort() }), reader.getRaw(1));
        assertEquals(ByteSourceFactory.wrap(str1Bytes), reader.getRaw(2));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyLong() >> 56), (byte) (pdx.getMyLong() >> 48), (byte) (pdx.getMyLong() >> 40), (byte) (pdx.getMyLong() >> 32), (byte) (pdx.getMyLong() >> 24), (byte) (pdx.getMyLong() >> 16), (byte) (pdx.getMyLong() >> 8), (byte) pdx.getMyLong() }), reader.getRaw(3));
        assertEquals(ByteSourceFactory.wrap(str2Bytes), reader.getRaw(4));
        assertEquals(ByteSourceFactory.wrap(str3Bytes), reader.getRaw(5));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (pdx.getMyInt() >> 24), (byte) (pdx.getMyInt() >> 16), (byte) (pdx.getMyInt() >> 8), (byte) pdx.getMyInt() }), reader.getRaw(6));
        assertEquals(ByteSourceFactory.wrap(new byte[] { (byte) (floatBytes >> 24), (byte) (floatBytes >> 16), (byte) (floatBytes >> 8), (byte) floatBytes }), reader.getRaw(7));
    } finally {
        c.setReadSerialized(false);
    }
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) PdxReaderImpl(org.apache.geode.pdx.internal.PdxReaderImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 97 with DataInput

use of java.io.DataInput in project gora by apache.

the class TestWritableUtils method testWritesReads.

@Test
public void testWritesReads() throws Exception {
    Properties props = new Properties();
    props.put("keyBlah", "valueBlah");
    props.put("keyBlah2", "valueBlah2");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutput out = new DataOutputStream(bos);
    WritableUtils.writeProperties(out, props);
    ((DataOutputStream) out).flush();
    DataInput in = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    Properties propsRead = WritableUtils.readProperties(in);
    assertEquals(propsRead.get("keyBlah"), props.get("keyBlah"));
    assertEquals(propsRead.get("keyBlah2"), props.get("keyBlah2"));
}
Also used : DataInput(java.io.DataInput) DataOutput(java.io.DataOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 98 with DataInput

use of java.io.DataInput in project ignite by apache.

the class HadoopSerializationWrapperSelfTest method testIntWritableSerialization.

/**
     * Tests read/write of IntWritable via native WritableSerialization.
     * @throws Exception If fails.
     */
public void testIntWritableSerialization() throws Exception {
    HadoopSerialization ser = new HadoopSerializationWrapper(new WritableSerialization(), IntWritable.class);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    DataOutput out = new DataOutputStream(buf);
    ser.write(out, new IntWritable(3));
    ser.write(out, new IntWritable(-5));
    assertEquals("[0, 0, 0, 3, -1, -1, -1, -5]", Arrays.toString(buf.toByteArray()));
    DataInput in = new DataInputStream(new ByteArrayInputStream(buf.toByteArray()));
    assertEquals(3, ((IntWritable) ser.read(in, null)).get());
    assertEquals(-5, ((IntWritable) ser.read(in, null)).get());
}
Also used : DataInput(java.io.DataInput) DataOutput(java.io.DataOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) HadoopSerializationWrapper(org.apache.ignite.internal.processors.hadoop.impl.v2.HadoopSerializationWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HadoopSerialization(org.apache.ignite.internal.processors.hadoop.HadoopSerialization) DataInputStream(java.io.DataInputStream) WritableSerialization(org.apache.hadoop.io.serializer.WritableSerialization) IntWritable(org.apache.hadoop.io.IntWritable)

Example 99 with DataInput

use of java.io.DataInput in project ignite by apache.

the class HadoopV2JobSelfTest method testCustomSerializationApplying.

/**
     * Tests that {@link HadoopJobEx} provides wrapped serializer if it's set in configuration.
     *
     * @throws IgniteCheckedException If fails.
     */
public void testCustomSerializationApplying() throws IgniteCheckedException {
    JobConf cfg = new JobConf();
    cfg.setMapOutputKeyClass(IntWritable.class);
    cfg.setMapOutputValueClass(Text.class);
    cfg.set(CommonConfigurationKeys.IO_SERIALIZATIONS_KEY, CustomSerialization.class.getName());
    HadoopDefaultJobInfo info = createJobInfo(cfg);
    final UUID uuid = UUID.randomUUID();
    HadoopJobId id = new HadoopJobId(uuid, 1);
    HadoopJobEx job = info.createJob(HadoopV2Job.class, id, log, null, new HadoopHelperImpl());
    HadoopTaskContext taskCtx = job.getTaskContext(new HadoopTaskInfo(HadoopTaskType.MAP, null, 0, 0, null));
    HadoopSerialization ser = taskCtx.keySerialization();
    assertEquals(HadoopSerializationWrapper.class.getName(), ser.getClass().getName());
    DataInput in = new DataInputStream(new ByteArrayInputStream(new byte[0]));
    assertEquals(TEST_SERIALIZED_VALUE, ser.read(in, null).toString());
    ser = taskCtx.valueSerialization();
    assertEquals(HadoopSerializationWrapper.class.getName(), ser.getClass().getName());
    assertEquals(TEST_SERIALIZED_VALUE, ser.read(in, null).toString());
}
Also used : HadoopHelperImpl(org.apache.ignite.internal.processors.hadoop.HadoopHelperImpl) DataInputStream(java.io.DataInputStream) HadoopJobId(org.apache.ignite.internal.processors.hadoop.HadoopJobId) DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) HadoopJobEx(org.apache.ignite.internal.processors.hadoop.HadoopJobEx) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) HadoopTaskInfo(org.apache.ignite.internal.processors.hadoop.HadoopTaskInfo) HadoopDefaultJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopDefaultJobInfo) HadoopSerializationWrapper(org.apache.ignite.internal.processors.hadoop.impl.v2.HadoopSerializationWrapper) UUID(java.util.UUID) HadoopSerialization(org.apache.ignite.internal.processors.hadoop.HadoopSerialization) JobConf(org.apache.hadoop.mapred.JobConf)

Example 100 with DataInput

use of java.io.DataInput in project incubator-systemml by apache.

the class PartitionedBlock method readExternal.

/**
	 * Redirects the default java serialization via externalizable to our default 
	 * hadoop writable serialization for efficient broadcast deserialization. 
	 * 
	 * @param is object input
	 * @throws IOException if IOException occurs
	 */
public void readExternal(ObjectInput is) throws IOException {
    DataInput dis = is;
    int code = readHeader(dis);
    if (is instanceof ObjectInputStream && code == 0) {
        // Apply only for MatrixBlock at this point as a temporary workaround
        // We will generalize this code by adding UTF functionality to support Frame
        //fast deserialize of dense/sparse blocks
        ObjectInputStream ois = (ObjectInputStream) is;
        dis = new FastBufferedDataInputStream(ois);
    }
    readPayload(dis, code);
}
Also used : DataInput(java.io.DataInput) ObjectInputStream(java.io.ObjectInputStream) FastBufferedDataInputStream(org.apache.sysml.runtime.util.FastBufferedDataInputStream)

Aggregations

DataInput (java.io.DataInput)238 Test (org.junit.Test)131 DataOutputStream (java.io.DataOutputStream)117 DataInputStream (java.io.DataInputStream)109 ByteArrayInputStream (java.io.ByteArrayInputStream)105 UnitTest (org.apache.geode.test.junit.categories.UnitTest)96 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 DataOutput (java.io.DataOutput)23 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)14 ARecord (org.apache.asterix.om.base.ARecord)14 EOFException (java.io.EOFException)9 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)7 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)7 GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)7 FileInputStream (java.io.FileInputStream)6 ByteBuffer (java.nio.ByteBuffer)6 HashMap (java.util.HashMap)6