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, int off, int len) throws IOException 

Source Link

Document

Writes a sub 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("java2s.com".getBytes(), 1, 2);
    out.close();/*  w  ww .  j ava 2s. co 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:SignatureTest.java

public static void main(String[] args) {
    try {//  www. j a v a  2s  .co  m
        if (args[0].equals("-genkeypair")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA");
            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("-sign")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            PrivateKey privkey = (PrivateKey) keyIn.readObject();
            keyIn.close();

            Signature signalg = Signature.getInstance("DSA");
            signalg.initSign(privkey);

            File infile = new File(args[1]);
            InputStream in = new FileInputStream(infile);
            int length = (int) infile.length();
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            signalg.update(message);
            byte[] signature = signalg.sign();

            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            int signlength = signature.length;
            out.writeInt(signlength);
            out.write(signature, 0, signlength);
            out.write(message, 0, length);
            out.close();
        } else if (args[0].equals("-verify")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));
            PublicKey pubkey = (PublicKey) keyIn.readObject();
            keyIn.close();

            Signature verifyalg = Signature.getInstance("DSA");
            verifyalg.initVerify(pubkey);

            File infile = new File(args[1]);
            DataInputStream in = new DataInputStream(new FileInputStream(infile));
            int signlength = in.readInt();
            byte[] signature = new byte[signlength];
            in.read(signature, 0, signlength);

            int length = (int) infile.length() - signlength - 4;
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            verifyalg.update(message);
            if (!verifyalg.verify(signature))
                System.out.print("not ");
            System.out.println("verified");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private synchronized void writeObject(ObjectOutputStream stream) throws IOException {
    stream.writeInt(getLength());/*from   ww w  .j av  a  2  s. com*/
    stream.write(getBytes(), 0, getLength());
}

From source file:org.nuxeo.ecm.core.api.SerializableInputStream.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();/*from ww w .j  a v  a  2  s  .com*/
    // write content
    if (in == null) {
        return;
    }
    try {
        int read;
        byte[] buf = new byte[IN_MEM_LIMIT];
        while ((read = in.read(buf)) != -1) {
            // next follows a chunk of 'read' bytes
            out.writeInt(read);
            out.write(buf, 0, read);
        }
        out.writeInt(-1); // EOF
    } finally {
        if (in != null) {
            in.close();
        }
    }
}