// // example program using Data streams, ByteArray streams // import java.io.* ; public class TestDataStreams { // first uses DataOutputStream, ByteArrayOutputStream to write // several primitive types to a byte array. // next prints the contents of the byte array. // then uses DataInputStream, ByteArrayInputStream // to read them back in in their original form. public static void main(String[] args) { // first write things out ------------------------------ // destination of output is a byte array, written by // this stream ByteArrayOutputStream bytesOut = new ByteArrayOutputStream() ; // data is sent into bytesOut stream by this filter // stream DataOutputStream dataOut = new DataOutputStream(bytesOut) ; // write out one or two values of each of several // primitive types; flush stream at end to be // sure no data is in limbo anywhere try { System.out.println("Writing data: ") ; dataOut.writeBoolean(true) ; System.out.println("\tBoolean " + true) ; dataOut.writeBoolean(false) ; System.out.println("\tBoolean " + false) ; dataOut.writeChar('A') ; System.out.println("\tChar" + 'A') ; dataOut.writeChar('Z') ; System.out.println("\tChar" + 'Z') ; dataOut.writeDouble(2.2) ; System.out.println("\tDouble " + 2.2) ; dataOut.writeFloat(4.4f) ; System.out.println("\tFloat " + 4.4f) ; dataOut.writeInt(6) ; System.out.println("\tInt " + 6) ; dataOut.writeLong(8l) ; System.out.println("\tLong " + 8l) ; dataOut.flush() ; } catch (IOException e) { System.out.println("Error writing data") ; } // now see what we wrote ------------------------------- // get byte array with contents of bytesOut buffer byte[] bytes = bytesOut.toByteArray() ; // print contents of byte array in hex, so we can see // what the DataOutputStream methods are doing System.out.println("Bytes written (in hex):") ; System.out.print("\t") ; for (int i = 0 ; i < bytes.length ; i++) // System.out.print(byteToHex(bytes[i]) + " ") ; System.out.println() ; // and now read back in -------------------------------- // source of input is byte array we just wrote, read // by this input stream ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes) ; // data is read from bytesIn stream by this filter // stream DataInputStream dataIn = new DataInputStream(bytesIn) ; // read data back into same primitive types we wrote // it from try { System.out.println("Data read:") ; System.out.println("\tBoolean " + dataIn.readBoolean()) ; System.out.println("\tBoolean " + dataIn.readBoolean()) ; System.out.println("\tChar " + dataIn.readChar()) ; System.out.println("\tChar " + dataIn.readChar()) ; System.out.println("\tDouble " + dataIn.readDouble()) ; System.out.println("\tFloat " + dataIn.readFloat()) ; System.out.println("\tInt " + dataIn.readInt()) ; System.out.println("\tLong " + dataIn.readLong()) ; // check for (expected) end of input if (dataIn.read() == -1) System.out.println("\tEnd of input " + "found") ; else System.out.println("\tEnd of input " + "not found") ; } catch (IOException e) { System.out.println("Error reading data") ; } } // convert a byte to a 2-character string representing its // hexadecimal value, with leading zero if needed static String byteToHex(byte b) { String temp = Integer.toHexString(b & 0xff) ; // toHexString takes an int argument; // "& 0xff" masks off sign-bit extension that // takes place when byte is converted to int if (temp.length() < 2) return "0" + temp ; else return temp ; } }