Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

In this page you can find the example usage for java.io ObjectOutputStream ObjectOutputStream.

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:com.github.stephanarts.cas.ticket.registry.provider.AddMethodTest.java

@Test
public void testValidInput() throws Exception {
    final byte[] serializedTicket;
    final HashMap<Integer, Ticket> map = new HashMap<Integer, Ticket>();
    final JSONObject params = new JSONObject();
    final IMethod method = new AddMethod(map);

    final String ticketId = "ST-1234567890ABCDEFGHIJKL-crud";
    final ServiceTicket ticket = mock(ServiceTicket.class, withSettings().serializable());
    when(ticket.getId()).thenReturn(ticketId);

    try {//from w  w  w. j a  v a  2s .c  om
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(ticket);
        so.flush();
        serializedTicket = bo.toByteArray();

        params.put("ticket-id", ticketId);
        params.put("ticket", DatatypeConverter.printBase64Binary(serializedTicket));
        method.execute(params);

    } catch (final JSONRPCException e) {
        Assert.fail(e.getMessage());
    } catch (final Exception e) {
        throw new Exception(e);
    }
}

From source file:geva.Main.State.java

/**
 * Save the State/*from w  w w. ja v a  2  s . c o m*/
 */
@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
public void save() {
    FileOutputStream fOut;
    ObjectOutputStream oOut;
    try {
        fOut = new FileOutputStream("saveFile");
        oOut = new ObjectOutputStream(fOut);
        oOut.writeObject(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.googlecode.jeeunit.example.test.spring.AuthorTest.java

/**
 * Test case for Glassfish <a//w w w . ja v a  2  s.com
 * href="https://glassfish.dev.java.net/issues/show_bug.cgi?id=12599">bug #12599</a>.
 * 
 * @throws IOException
 * @throws ClassNotFoundException
 */
@Test
@Ignore
public void serialization() throws IOException, ClassNotFoundException {
    long expectedNumBooks = libraryService.getNumBooks();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(libraryService);
    oos.close();

    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object obj = ois.readObject();
    assertTrue(obj instanceof LibraryService);

    // the deserialized proxy throws a NullPointerException on method invocation
    long numBooks = ((LibraryService) obj).getNumBooks();
    assertEquals(expectedNumBooks, numBooks);
}

From source file:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized./*  ww  w . jav  a  2s. co  m*/
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamUtils.closeOutputStream(byteArrayOutputStream);
        StreamUtils.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:com.conwet.silbops.model.ContextFunctionTest.java

@Test
public void shouldExternalize() throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput output = new ObjectOutputStream(baos);

    output.writeObject(function);//  w  w w .j av a 2  s  .  c o m
    output.close();

    ObjectInput input = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

    ContextFunction cxtFun = (ContextFunction) input.readObject();
    assertThat(cxtFun).isEqualTo(function);
}

From source file:com.github.stephanarts.cas.ticket.registry.provider.UpdateMethodTest.java

@Test
public void testValidInput() throws Exception {
    final byte[] serializedTicket;
    final HashMap<Integer, Ticket> map = new HashMap<Integer, Ticket>();
    final JSONObject params = new JSONObject();
    final IMethod method = new UpdateMethod(map);

    final String ticketId = "ST-1234567890ABCDEFGHIJKL-crud";
    final ServiceTicket ticket = mock(ServiceTicket.class, withSettings().serializable());
    when(ticket.getId()).thenReturn(ticketId);

    map.put(ticketId.hashCode(), ticket);

    try {//from w  ww.ja  va  2 s .com
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream so = new ObjectOutputStream(bo);
        so.writeObject(ticket);
        so.flush();
        serializedTicket = bo.toByteArray();

        params.put("ticket-id", ticketId);
        params.put("ticket", DatatypeConverter.printBase64Binary(serializedTicket));
        method.execute(params);

    } catch (final JSONRPCException e) {
        Assert.fail(e.getMessage());
    } catch (final Exception e) {
        throw new Exception(e);
    }
}

From source file:com.tecapro.inventory.common.util.CommonUtil.java

/**
 * encodeBase64 object//from w w w . j a v a 2s  . c  om
 *
 * @param obj
 * @return
 * @throws Exception
 */
public byte[] serialize(Object obj) throws Exception {
    ByteArrayOutputStream byteStream = null;
    ObjectOutputStream ostream = null;
    byte[] result = null;

    try {
        byteStream = new ByteArrayOutputStream();
        ostream = new ObjectOutputStream(new BufferedOutputStream(byteStream));
        ostream.writeObject(obj);
        ostream.flush();
        result = Base64.encodeBase64(byteStream.toByteArray());
    } finally {
        if (byteStream != null) {
            byteStream.close();
        }
        if (ostream != null) {
            ostream.close();
        }
    }
    return result;
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Copys the given Object and returns the copy from the object or null if the object can't be
 * serialized.//from w ww . jav  a 2  s  .c  om
 *
 * @param <T>
 *            the generic type of the given object
 * @param orig
 *            The object to copy.
 * @return Returns a copy from the original object.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
@SuppressWarnings("unchecked")
public static <T extends Serializable> T copySerializedObject(final T orig)
        throws IOException, ClassNotFoundException {
    T object = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(orig);
        objectOutputStream.flush();
        objectOutputStream.close();
        final ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream ois = new ObjectInputStream(bis);
        object = (T) ois.readObject();
    } finally {
        StreamExtensions.closeOutputStream(byteArrayOutputStream);
        StreamExtensions.closeOutputStream(objectOutputStream);
    }
    return object;
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;//www  .ja  v  a 2 s  . co m
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

From source file:net.darkmist.alib.io.Serializer.java

/** Write one object serilized to a output stream.
 * @param obj The Object to serilize./*from w w w  .j a v  a 2s.  c  o m*/
 * @param out The output stream to write it to. This will be closed!
 */
public static <T extends Serializable> void serializeToStream(T obj, OutputStream out) throws IOException {
    ObjectOutputStream objOut;

    if (out instanceof ObjectOutputStream)
        objOut = (ObjectOutputStream) out;
    else
        objOut = new ObjectOutputStream(out);
    objOut.writeObject(obj);
    objOut.close();
}