RE: Java Streams

"Andy Burns" <[email protected]> Wed, 4 Dec 2002 17:48:23 -0000
Newsgroups gmane.network.beep.beepcore.java.general
Organization Protologic Ltd
Message-ID <[email protected]>
> 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  [email protected]
 *
 */

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;
    }
  
  }



-------------------------------------------------------
This SF.net email is sponsored by: Microsoft Visual Studio.NET 
comprehensive development tool, built to increase your 
productivity. Try a free online hosted session at:
http://ads.sourceforge.net/cgi-bin/redirect.pl?micr0003en