Example usage for java.io ObjectOutputStream write

List of usage examples for java.io ObjectOutputStream write

Introduction

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

Prototype

public void write(byte[] buf) throws IOException 

Source Link

Document

Writes an array of bytes.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.write(1);
    out.close();/*ww  w.j av a 2  s. c om*/

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.write("java2s.com".getBytes());
    out.close();//from  www  .  j  av  a2s  .c  o  m

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:RSATest.java

public static void main(String[] args) {
    try {//www .  j a  va2  s  . co m
        if (args[0].equals("-genkey")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();
        } else if (args[0].equals("-encrypt")) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();

            // wrap with RSA public key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key publicKey = (Key) keyIn.readObject();
            keyIn.close();

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.WRAP_MODE, publicKey);
            byte[] wrappedKey = cipher.wrap(key);
            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            out.writeInt(wrappedKey.length);
            out.write(wrappedKey);

            InputStream in = new FileInputStream(args[1]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            crypt(in, out, cipher);
            in.close();
            out.close();
        } else {
            DataInputStream in = new DataInputStream(new FileInputStream(args[1]));
            int length = in.readInt();
            byte[] wrappedKey = new byte[length];
            in.read(wrappedKey, 0, length);

            // unwrap with RSA private key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key privateKey = (Key) keyIn.readObject();
            keyIn.close();

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.UNWRAP_MODE, privateKey);
            Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);

            OutputStream out = new FileOutputStream(args[2]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);

            crypt(in, out, cipher);
            in.close();
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:llc.rockford.webcast.StringUtils.java

public static String encodeBase64BinaryFile(InputStream is) {
    String base64EncodedFile = "";
    try {//from   w ww  .j av a2s  . c o  m
        byte[] bytesFromFile = IOUtils.toByteArray(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(baos);
        stream.write(bytesFromFile);
        stream.flush();
        stream.close();
        base64EncodedFile = new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncodedFile;
}

From source file:org.eclipse.wb.internal.core.utils.IOUtils2.java

/**
 * Writes byte array as length and bytes.
 *//*from   w w  w .  j av a  2s  . c om*/
public static void writeByteArray(ObjectOutputStream oos, byte[] bytes) throws IOException {
    oos.writeInt(bytes.length);
    oos.write(bytes);
}

From source file:Main.java

public static void writeStroke(Stroke aStroke, ObjectOutputStream out) throws IOException {
    if (aStroke instanceof Serializable) {
        out.writeBoolean(true);// w w  w.j  a v a  2  s.c o  m
        out.writeBoolean(true);
        out.writeObject(aStroke);
    } else if (aStroke instanceof BasicStroke) {
        out.writeBoolean(true);
        out.writeBoolean(false);
        BasicStroke s = (BasicStroke) aStroke;

        float[] dash = s.getDashArray();

        if (dash == null) {
            out.write(0);
        } else {
            out.write(dash.length);
            for (int i = 0; i < dash.length; i++) {
                out.writeFloat(dash[i]);
            }
        }

        out.writeFloat(s.getLineWidth());
        out.writeInt(s.getEndCap());
        out.writeInt(s.getLineJoin());
        out.writeFloat(s.getMiterLimit());
        out.writeFloat(s.getDashPhase());
    } else {
        out.writeBoolean(false);
    }
}

From source file:org.apache.flink.api.common.accumulators.ListAccumulator.java

@Override
public void write(ObjectOutputStream out) throws IOException {
    int numItems = localValue.size();
    out.writeInt(numItems);/* www.  ja  v  a2 s .  c o m*/
    for (byte[] item : localValue) {
        out.writeInt(item.length);
        out.write(item);
    }
}

From source file:mitm.application.djigzo.james.EncryptedContainer.java

private void writeObject(ObjectOutputStream out) throws IOException, EncryptorException {
    out.writeLong(serialVersionUID);// w ww . ja v a  2  s.  c om

    byte[] encrypted = getEncryptor().encrypt(SerializationUtils.serialize(value));

    out.writeInt(ArrayUtils.getLength(encrypted));
    out.write(encrypted);
}

From source file:com.happyblueduck.lembas.datastore.LembasEntity.java

private void writeObject(java.io.ObjectOutputStream out) throws IOException {

    try {/*from   w  ww. j  av a 2s  .c  o  m*/
        org.json.simple.JSONObject result = LembasUtil.serialize(this, false);
        String jsonString = result.toJSONString();
        out.write(jsonString.getBytes());
        //logger.info("> serializing"+ this.getClassName() +":"+ jsonString);
    } catch (UtilSerializeException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        throw new IOException();

    }

}

From source file:org.spout.api.inventory.ItemStack.java

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
    out.writeShort(material.getId());//ww w .  ja  va 2 s  .  c o  m
    out.writeShort(material.getData());
    out.writeInt(amount);
    out.writeShort(data);
    byte[] auxData = this.auxData.serialize();
    if (auxData != null) {
        out.writeInt(auxData.length);
        out.write(auxData);
    } else {
        out.writeInt(0);
    }

    if (nbtData != null && !nbtData.isEmpty()) {
        out.writeBoolean(true);
        NBTOutputStream os = new NBTOutputStream(out, false);
        os.writeTag(new CompoundTag("nbtData", nbtData));
        os.close();
    } else {
        out.writeBoolean(false);
    }
}