Search in sources :

Example 31 with ObjectOutputStream

use of java.io.ObjectOutputStream in project gradle by gradle.

the class PayloadSerializer method serialize.

public SerializedPayload serialize(Object payload) {
    final SerializeMap map = classLoaderRegistry.newSerializeSession();
    try {
        StreamByteBuffer buffer = new StreamByteBuffer();
        final ObjectOutputStream objectStream = new PayloadSerializerObjectOutputStream(buffer.getOutputStream(), map);
        try {
            objectStream.writeObject(payload);
        } finally {
            IoActions.closeQuietly(objectStream);
        }
        Map<Short, ClassLoaderDetails> classLoaders = new HashMap<Short, ClassLoaderDetails>();
        map.collectClassLoaderDefinitions(classLoaders);
        return new SerializedPayload(classLoaders, buffer.readAsListOfByteArrays());
    } catch (IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : HashMap(java.util.HashMap) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 32 with ObjectOutputStream

use of java.io.ObjectOutputStream in project grails-core by grails.

the class StreamCharBufferTests method testSerialization.

public void testSerialization() throws Exception {
    StreamCharBuffer charBuffer = new StreamCharBuffer();
    Writer writer = charBuffer.getWriter();
    writer.write("ABCDE");
    writer.flush();
    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(charBuffer);
    out.close();
    byte[] bytes = bos.toByteArray();
    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
    charBuffer = (StreamCharBuffer) in.readObject();
    assertEquals("ABCDE", charBuffer.toString());
    assertEquals(5, charBuffer.size());
    charBuffer.getWriter().write("12345");
    assertEquals("ABCDE12345", charBuffer.toString());
    assertEquals(10, charBuffer.size());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) StringWriter(java.io.StringWriter) Writer(java.io.Writer) CharArrayWriter(java.io.CharArrayWriter) ObjectInputStream(java.io.ObjectInputStream)

Example 33 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class AnnotationTypeMismatchExceptionTest method testSerialization.

public void testSerialization() throws Exception {
    Method m = String.class.getMethod("length");
    AnnotationTypeMismatchException original = new AnnotationTypeMismatchException(m, "poop");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        // AnnotationTypeMismatchException is broken: it's Serializable but has a non-transient
        // non-serializable field of type Method.
        new ObjectOutputStream(out).writeObject(original);
        fail();
    } catch (NotSerializableException expected) {
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) AnnotationTypeMismatchException(java.lang.annotation.AnnotationTypeMismatchException)

Example 34 with ObjectOutputStream

use of java.io.ObjectOutputStream in project j2objc by google.

the class SerializationTester method getDeserilizedObject.

/*
	 * -------------------------------------------------------------------
	 * Methods
	 * -------------------------------------------------------------------
	 */
/**
	 * Serialize an object and then deserialize it.
	 *
	 * @param inputObject
	 *            the input object
	 * @return the deserialized object
	 */
public static Object getDeserilizedObject(Object inputObject) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(inputObject);
    oos.close();
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bis);
    Object outputObject = ois.readObject();
    lastOutput = outputObject;
    ois.close();
    return outputObject;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 35 with ObjectOutputStream

use of java.io.ObjectOutputStream in project error-prone by google.

the class RefasterRuleCompilerAnalyzer method finished.

@Override
public void finished(TaskEvent taskEvent) {
    if (taskEvent.getKind() != Kind.ANALYZE) {
        return;
    }
    if (JavaCompiler.instance(context).errorCount() > 0) {
        return;
    }
    ClassTree tree = JavacTrees.instance(context).getTree(taskEvent.getTypeElement());
    if (tree == null) {
        return;
    }
    Collection<? extends CodeTransformer> rules = RefasterRuleBuilderScanner.extractRules(tree, context);
    try (ObjectOutputStream output = new ObjectOutputStream(Files.newOutputStream(destinationPath))) {
        output.writeObject(CompositeCodeTransformer.compose(rules));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassTree(com.sun.source.tree.ClassTree) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)1102 ByteArrayOutputStream (java.io.ByteArrayOutputStream)760 ObjectInputStream (java.io.ObjectInputStream)428 ByteArrayInputStream (java.io.ByteArrayInputStream)375 IOException (java.io.IOException)358 FileOutputStream (java.io.FileOutputStream)161 Test (org.junit.Test)161 File (java.io.File)96 BufferedOutputStream (java.io.BufferedOutputStream)52 ObjectOutput (java.io.ObjectOutput)47 OutputStream (java.io.OutputStream)37 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)25 InputStream (java.io.InputStream)23 FileNotFoundException (java.io.FileNotFoundException)22 Serializable (java.io.Serializable)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)16 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14