Example usage for java.io ObjectOutput writeObject

List of usage examples for java.io ObjectOutput writeObject

Introduction

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

Prototype

public void writeObject(Object obj) throws IOException;

Source Link

Document

Write an object to the underlying storage or stream.

Usage

From source file:com.conwet.silbops.msg.UnadvertiseMsgTest.java

@Test
public void shouldExternalize() throws Exception {
    UnadvertiseMsg unAdvMsg = new UnadvertiseMsg();

    Advertise subscription = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG)
            .attribute("attr3", Type.STRING);
    unAdvMsg.setAdvertise(subscription);

    Advertise uncovered1 = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG)
            .attribute("attr3", Type.STRING).attribute("attr4", Type.DOUBLE);

    Advertise uncovered2 = new Advertise().attribute("attr1", Type.DOUBLE).attribute("attr2", Type.LONG)
            .attribute("attr3", Type.STRING).attribute("attr5", Type.LONG);

    Set<Advertise> uncovered = new HashSet<>();
    uncovered.add(uncovered1);/*from ww  w.ja  va  2 s.  co  m*/
    uncovered.add(uncovered2);
    unAdvMsg.setUncovered(uncovered);

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

    output.writeObject(unAdvMsg);
    output.close();

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

    assertThat((UnadvertiseMsg) input.readObject()).isEqualTo(unAdvMsg);
}

From source file:com.conwet.silbops.msg.UnsubscribeMsgTest.java

@Test
public void shouldExternalizeAndDesexternalize() throws Exception {
    UnsubscribeMsg unSubMsg = new UnsubscribeMsg();

    Subscription subscription = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D)
            .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST")
            .subscription();//from w  ww  .j  av  a 2 s  .  c  om
    unSubMsg.setSubscription(subscription);

    Subscription uncovered1 = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D)
            .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST")
            .constrain("attr4", Type.DOUBLE).le(7D).subscription();

    Subscription uncovered2 = new Subscription().constrain("attr1", Type.DOUBLE).ge(20.5D)
            .constrain("attr2", Type.LONG).eq(2L).constrain("attr3", Type.STRING).contains("TEST")
            .constrain("attr4", Type.LONG).gt(7L).subscription();

    Set<Subscription> uncovered = new HashSet<>();
    uncovered.add(uncovered1);
    uncovered.add(uncovered2);
    unSubMsg.setUncovered(uncovered);

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

    output.writeObject(unSubMsg);
    output.close();

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

    assertThat((UnsubscribeMsg) input.readObject()).isEqualTo(unSubMsg);
}

From source file:com.rr.familyPlanning.ui.security.encryptObject.java

/**
 * Encrypts and encodes the Object and IV for url inclusion
 *
 * @param input/* w  w  w  .  j a  v a  2  s . co m*/
 * @return
 * @throws Exception
 */
public String[] encryptObject(Object obj) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    try {
        // Serialize the object
        out.writeObject(obj);
        byte[] serialized = stream.toByteArray();
        // Setup the cipher and Init Vector
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] iv = new byte[cipher.getBlockSize()];
        new SecureRandom().nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        // Hash the key with SHA-256 and trim the output to 128-bit for the key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(keyString.getBytes());
        byte[] key = new byte[16];
        System.arraycopy(digest.digest(), 0, key, 0, key.length);
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        // encrypt
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        // Encrypt & Encode the input
        byte[] encrypted = cipher.doFinal(serialized);
        byte[] base64Encoded = Base64.encodeBase64(encrypted);
        String base64String = new String(base64Encoded);
        String urlEncodedData = URLEncoder.encode(base64String, "UTF-8");
        // Encode the Init Vector
        byte[] base64IV = Base64.encodeBase64(iv);
        String base64IVString = new String(base64IV);
        String urlEncodedIV = URLEncoder.encode(base64IVString, "UTF-8");

        return new String[] { urlEncodedData, urlEncodedIV };
    } finally {
        stream.close();
        out.close();
    }
}

From source file:org.appverse.web.framework.backend.core.enterprise.aop.managers.impl.live.ProfileManagerImpl.java

private String getObjectSize(Object object) throws IOException {
    if (object instanceof Serializable) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(object);
        out.close();//www  .j  a v a 2 s . com

        byte[] buf = bos.toByteArray();
        return String.valueOf(buf.length);
    }
    return "Unknow";
}

From source file:bftsmart.consensus.TimestampValuePair.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {

    out.writeInt(timestamp);/*ww  w.  j  a  v a 2 s. co  m*/
    out.writeObject(value);
}

From source file:org.nuxeo.ecm.platform.ui.web.binding.MetaValueExpression.java

public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(originalValueExpression);
}

From source file:com.ps.cc.controller.Init.java

@ActionMapping(params = "action=saveTwilioConfiguration")
public void saveTwilioConfiguration(@ModelAttribute TwilioConfiguration twilioConfiguration,
        ActionRequest actionRequest) throws Exception {
    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("twilioConfiguration.txt"));
    out.writeObject(twilioConfiguration);
    out.close();//from   w ww .j a v a 2 s  . c  om

    User user = PortalUtil.getUser(actionRequest);

    TwilioCapability capability = new TwilioCapability(twilioConfiguration.getAcctSid(),
            twilioConfiguration.getAuthToken());

    capability.allowEventStream(null);
    capability.allowClientIncoming(user.getScreenName());

    Map<String, String> params = new HashMap<String, String>();
    params.put("portraitId", String.valueOf(user.getPortraitId()));

    capability.allowClientOutgoing(twilioConfiguration.getAppSid(), params);

    String token = capability.generateToken();

    PortalUtil.getHttpServletRequest(actionRequest).getSession().setAttribute("USER_twilio_token", token);
}

From source file:com.github.steveash.jg2p.align.ProbTable.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(this.xyProb);
}

From source file:com.adaptris.core.interceptor.MetadataStatistic.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeLong(getEndMillis());/*  ww  w  .java 2 s. c o m*/
    out.writeObject(getMetadataStatistics());
    out.writeLong(getStartMillis());
}

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

@Test
public void shouldExternalize() throws Exception {

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

    output.writeObject(advertise);
    output.close();// ww w . j ava  2s.c om

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

    assertThat((Advertise) input.readObject()).isEqualTo(advertise);
}