RE: [Beepcore-java-users] Java Streams
Status: Beta
Brought to you by:
huston
From: Andy B. <and...@pr...> - 2002-12-04 17:42:35
|
> Unfortunately I haven't > gotten around to doing the OutputStream version but maybe it is time I > addressed this and the FileOutputDataStream... I've implemented a version of OutputDataStreamAdapter which is probably inefficient, bugridden and lacking in documentatin but it's a start ;-) It assumes you will set the MimeHeaders via the OutputDataStream before creating an OutputDataStreamAdapter from it. I've sucessfully used it to pass a Vector of strings and integers. My example code which is the run method of my profile reply thread .... Vector result = new Vector(); MimeHeaders mh = new MimeHeaders(); OutputDataStream ods = new OutputDataStream(mh); objStream = new ObjectOutputStream(new OutputDataStreamAdapter(ods)); result.add(new Integer(123)); result.add(new String("hello")); objStream.writeObject(result); objStream.close(); message.sendRPY(ods); If I tidy this up a bit is it worthy of inclusion into beepcore? =========cut OutputDataStreamAdapter.java below ================ /* * OutputDataStreamAdapter.java * * Created on 04 December 2002, 10:49 * @author and...@pr... * */ package org.beepcore.beep.core; import java.io.IOException; import org.beepcore.beep.util.BufferSegment; import org.beepcore.beep.core.OutputDataStream; import org.beepcore.beep.core.BEEPException; public class OutputDataStreamAdapter extends java.io.OutputStream { static final int DEFAULT_BUFFER_SIZE = 1024; private OutputDataStream ods; private int dataSize; private byte[] dataBuffer; private int dataCount; /** Creates a new instance of OutputDataStreamAdapter */ public OutputDataStreamAdapter(OutputDataStream ods) { this(ods, DEFAULT_BUFFER_SIZE); } public OutputDataStreamAdapter(OutputDataStream ods, int bufferSize) { this.ods = ods; dataSize = bufferSize; dataCount = 0; dataBuffer = new byte[dataSize]; } /** Writes the specified byte to this output stream. The general * contract for <code>write</code> is that one byte is written * to the output stream. The byte to be written is the eight * low-order bits of the argument <code>b</code>. The 24 * high-order bits of <code>b</code> are ignored. * <p> * Subclasses of <code>OutputStream</code> must provide an * implementation for this method. * * @param b the <code>byte</code>. * @exception IOException if an I/O error occurs. In particular, * an <code>IOException</code> may be thrown if the * output stream has been closed. * */ public void write(int b) throws IOException { if (dataCount > dataSize) { // whoops!!! } if (dataCount == dataSize) { this.flush(); } dataBuffer[dataCount++] = (byte)b; } public void flush() throws IOException { if (dataCount > 0) { BufferSegment bs = new BufferSegment(dataBuffer, 0, dataCount); ods.add(bs); dataBuffer = new byte[dataSize]; dataCount = 0; } } public void close() throws IOException { this.flush(); ods.setComplete(); dataBuffer = null; } } |