Search in sources :

Example 1 with PropertiesSerializer

use of org.apache.pivot.serialization.PropertiesSerializer in project pivot by apache.

the class PropertiesSerializerTest method readValues.

@Test
public // run writeValues before readValues, important
void readValues() throws IOException, SerializationException {
    log("readValues()");
    assertNotNull(testMap);
    // prepare test data, but without using a static variable
    byte[] testBytes = mapToPropertiesToBytes(testMap);
    assertNotNull(testBytes);
    Serializer<Map<?, ?>> serializer = new PropertiesSerializer();
    log("serializer instance created: " + serializer);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(testBytes);
    @SuppressWarnings("unchecked") Map<String, Object> readData = (Map<String, Object>) serializer.readObject(inputStream);
    assertNotNull(readData);
    log("Succesfully Read");
    for (String key : readData) {
        log(key + "=" + readData.get(key));
    }
    inputStream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PropertiesSerializer(org.apache.pivot.serialization.PropertiesSerializer) HashMap(org.apache.pivot.collections.HashMap) Map(org.apache.pivot.collections.Map) Test(org.junit.Test)

Example 2 with PropertiesSerializer

use of org.apache.pivot.serialization.PropertiesSerializer in project pivot by apache.

the class PropertiesSerializerTest method mapToPropertiesToBytes.

// utility method to transform the given Map to Properties,
// and then to byte array
protected byte[] mapToPropertiesToBytes(Map<String, Object> testMap2) throws IOException, SerializationException {
    Serializer<Map<?, ?>> serializer = new PropertiesSerializer();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    serializer.writeObject(testMap2, outputStream);
    outputStream.flush();
    outputStream.close();
    String result = outputStream.toString();
    return result.getBytes();
}
Also used : PropertiesSerializer(org.apache.pivot.serialization.PropertiesSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(org.apache.pivot.collections.HashMap) Map(org.apache.pivot.collections.Map)

Aggregations

HashMap (org.apache.pivot.collections.HashMap)2 Map (org.apache.pivot.collections.Map)2 PropertiesSerializer (org.apache.pivot.serialization.PropertiesSerializer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Test (org.junit.Test)1