Example usage for java.io ObjectInputStream read

List of usage examples for java.io ObjectInputStream read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data.

Usage

From source file:Main.java

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

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();//from  www . ja v a 2 s  .  c  om
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }
    ois.close();
}

From source file:Main.java

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

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from  www  .  j  a  v  a  2  s .  c  o  m*/

    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read from the stream
    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }

    ois.close();

}

From source file:Main.java

public static Stroke readStroke(ObjectInputStream in) throws IOException, ClassNotFoundException {
    boolean wroteStroke = in.readBoolean();
    if (wroteStroke) {
        boolean serializedStroke = in.readBoolean();
        if (serializedStroke) {
            return (Stroke) in.readObject();
        } else {/*ww w .  j  a  v a 2 s  .  c  om*/
            float[] dash = null;
            int dashLength = in.read();

            if (dashLength != 0) {
                dash = new float[dashLength];
                for (int i = 0; i < dashLength; i++) {
                    dash[i] = in.readFloat();
                }
            }

            float lineWidth = in.readFloat();
            int endCap = in.readInt();
            int lineJoin = in.readInt();
            float miterLimit = in.readFloat();
            float dashPhase = in.readFloat();

            return new BasicStroke(lineWidth, endCap, lineJoin, miterLimit, dash, dashPhase);
        }
    } else {
        return null;
    }
}

From source file:com.healthmarketscience.rmiio.DirectRemoteInputStream.java

/**
 * Reads the state of this object and all of the underlying stream's data
 * directly from the given ObjectInputStream. The stream data is stored in
 * a temporary file in the default java temp directory with the name {@code "stream_<num>.dat"}.
 *//* w  w  w.j  a v  a 2 s  .  c  om*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();

    // read the default chunk size from the incoming file
    final int defaultChunkSize = in.readInt();
    checkChunkSize(defaultChunkSize);

    // setup a temp file for the incoming data (make sure it gets cleaned up
    // somehow)
    _tmpFile = File.createTempFile("stream_", ".dat");
    _tmpFile.deleteOnExit();

    FileOutputStream out = new FileOutputStream(_tmpFile);
    try {
        // limit buffer size in case of malicious input
        byte[] transferBuf = new byte[Math.min(defaultChunkSize, RemoteInputStreamServer.DEFAULT_CHUNK_SIZE)];
        while (true) {

            // read in another chunk
            int chunkCode = in.read();
            if (chunkCode == EOF_CODE) {
                // all done
                break;
            }

            int readLen = defaultChunkSize;
            if (chunkCode != DEFAULT_CHUNK_CODE) {
                readLen = in.readInt();
                checkChunkSize(readLen);
            }

            // copy chunk into temp file
            copy(in, out, transferBuf, readLen);

        }

        // attempt to close the temp file. if successful, we're good to go
        out.close();

        // sweet, setup final state
        _monitor = RemoteInputStreamServer.DUMMY_MONITOR;
        _in = new BufferedInputStream(new FileInputStream(_tmpFile));

        // the underlying stream is now in it's initial state
        _consumptionState = ConsumptionState.NONE;
        _gotEOF = false;

    } finally {
        RmiioUtil.closeQuietly(out);
    }

}

From source file:org.scantegrity.crypto.FlatFileTable.java

public FlatFileTable(String p_path) throws FileNotFoundException, IOException, ClassNotFoundException {
    ObjectInputStream l_in = new ObjectInputStream(new FileInputStream(p_path));
    int l_size = l_in.read();

    c_list = new ArrayList<ArrayList<Object>>();

    while (l_in.available() > 0) {
        ArrayList<Object> l_row = new ArrayList<Object>();
        for (int x = 0; x < l_size; x++)
            l_row.add(l_in.readObject());
        c_list.add(l_row);/*w  w w. j  a v a 2 s . c om*/
    }
}