效能提高技巧之一:使用ByteArrayInputStream或ByteArrayOnputStream

banq發表於2002-12-10
和sokcet的結合

DataInputStream in = new DataInputStream ( socket.getInputStream ( ) );

byte[ ] barray = new Byte [ sizeNeeded ];

in.read ( barray );

ByteArrayInputStream bais = new ByteArrayInputStream ( barray );


物件可以透過資料包傳送:

Writing Object to stream
-------------------------
//create ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();

//create ObjectOutputStream to write objects
ObjectOutputStream oos = new ObjectOutputStream(baos);

//write the object to the stream
oos.writeObject(obj);

//flush the stream
oos.flush();

Reading Object from stream
-------------------------
//byte array input stream
ByteArrayInputStream bais = new ByteArrayInputStream(data);

//object input stream
ObjectInputStream ois = new ObjectInputStream(bais);

//object from the stream
ois.readObject();

相關文章