Thread: [Beepcore-java-users] Java Streams
Status: Beta
Brought to you by:
huston
From: Andy B. <and...@pr...> - 2002-12-03 15:37:46
|
Hi, We're using beepcore-java for our client/server communication on a "trial" project, so far so good, we've got a listener based on beepd and our own profiles. One thing that makes itself clear farly quickly is how much nicer it would be if beep's InputDataStream and OutputDataStream were derived from java's own Stream classes (or perhaps implemented their interfaces) In particular it would make it easier to send/receive XML documents or serialize entire java objects through beep channels without "stringizing" them first. Have I missed a trick that makes this possible already? Has anyone thought the same thoughts? How difficuly would it be to implement? Thanks .... |
From: Huston <hu...@us...> - 2002-12-04 02:51:03
|
> We're using beepcore-java for our client/server communication on a > "trial" project, so far so good, we've got a listener based on beepd and > our own profiles. > > One thing that makes itself clear farly quickly is how much nicer it > would be if beep's InputDataStream and OutputDataStream were derived > from java's own Stream classes (or perhaps implemented their interfaces) > > In particular it would make it easier to send/receive XML documents or > serialize entire java objects through beep channels without > "stringizing" them first. > > Have I missed a trick that makes this possible already? Has anyone > thought the same thoughts? How difficuly would it be to implement? You have a good point. In a previous version we tried doing what you suggested but ran into a couple of snags but I think the problem is better understood now and maybe we should revisit this decision. In the mean time there is an InputDataStreamAdapter (I know it is a really bad name, I keep hoping someone will suggest something better) that you can get from a InputDataStream that inherits from InputStream. Unfortunately I haven't gotten around to doing the OutputStream version but maybe it is time I addressed this and the FileOutputDataStream... --Huston |
From: Andy B. <and...@pr...> - 2002-12-04 10:41:07
|
> In the mean time there is an InputDataStreamAdapter I'll have a look at that, it sounds like it makes one end of the job easier ... > Unfortunately I haven't > gotten around to doing the OutputStream version but maybe it is time I > addressed this and the FileOutputDataStream... I might be tempted to look at InputDataStreamAdapter to gauge the complexity of writing OutputDataStreamAdapter .... |
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; } } |