superwaba.ext.xplat.io
Class BigByteArrayStream

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--superwaba.ext.xplat.io.BigByteArrayStream

public class BigByteArrayStream
extends Stream

Creates a big byte array stream, supporting more than 64kb of data. Arrays of 16000 bytes will be created and each byte array will be compressed once filled and will be automatically decompressed on read. This saves space but adds a slowdown to the process. This class requires the superwaba.ext.xplat.zlib package. It is useful when transferring FTP files to/from the server.

This class cannot be used for output AND input, but only for output OR input, in an absolutely sequential mode (the skipBytes method is NOT implemented): you must write everything, then read everything. To change the mode, use the setMode(READ_MODE or WRITE_MODE) method. No check is made to see if you're in the right mode, but your program will probably crash if you do it in the wrong one.

Sample that transfers bytes to the server:

 BigByteArrayStream bbas = new BigByteArrayStream(9); // default mode is WRITE_MODE
 for (int i =0; i < 50000; i++)
    bbas.writeLine("1234567890"); // already appends \r\n
 bbas.flush();
 bbas.setMode(BigByteArrayStream.READ_MODE); // prepare for read
 ftp.sendFile(bbas, "bigfile.txt",true);
 // if you want to send another one, just call bbas.setMode(BigByteArrayStream.WRITE_MODE);   
 
Sample that transfers bytes from the server:
 BigByteArrayStream bbas = new BigByteArrayStream(9);
 ftp.receiveFile("bigfile.txt", bbas);
 bbas.flush();
 String line;
 while ((line = bbas.readLine()) != null)
    // do something with the line!
 
Here is another fully functional sample:
 int i;
 String g = "1234567890";
 BigByteArrayStream bbas = new BigByteArrayStream(9); // default mode is WRITE_MODE
 for (i =0; i < 50000; i++)
    bbas.writeLine(g); // already appends \r\n
 bbas.flush();
 Vm.debug("size: "+bbas.getCompressedSize()+" -> "+bbas.getSize());
 String s;
 for (i=0; (s=bbas.readLine()) != null; i++)
    if (!g.equals(s))
       Vm.debug("erro em "+i);
 if (i != 50000)
    Vm.debug("i difere!");
 bbas.close();
 
Note that, although the sample uses writeLine and readLine, you can store any kind of data, by attaching a DataStream as
 BigByteArrayStream bbas = new BigByteArrayStream(5);
 DataStream ds = new DataStream(bbas);
 byte[] big = new byte[200000];
 // fill big with something
 ds.writeBytes(big);
 for (int i =0; i < 100000; i++)
 {
    ds.writeInt(0x123456);
    ds.writeString("Michelle");
    ds.writeDouble(123.456d);
 }
 // well, now we do something with these!
 int realSize = bbas.getSize(); // just for fun
 int compressed = bbas.getCompressedSize(); // just for fun
 ds.readBytes(big);
 for (int i =0; i < 100000; i++)
 {
    int i = ds.readInt();
    String love = ds.writeString(); // Michelle
    double d = ds.writeDouble();
 } 
 
Call the close method only when you're completely done in using it: all the internal buffers will be released, and reading from it will crash your program.

Note that the readLine method will not work if there are any character with accentuation.


Inner Class Summary
static class BigByteArrayStream.DirectCharConverter
           
 
Field Summary
static byte[] crlf
          Defines the line terminator, which is by default \r\n.
static int DESTRUCTIVE_READ_MODE
          used in the setMode method.
static int READ_MODE
          Used in the setMode method.
static int WRITE_MODE
          Used in the setMode method.
 
Constructor Summary
BigByteArrayStream(int compressionLevel)
          Creates a new BigByteArrayStream, using the given compression level (0 = no compression, 9 = max compression).
 
Method Summary
 boolean close()
          Deletes all internal buffers.
 void flush()
          Commits the last part of the file that didn't achieved the limits.
 int getCompressedSize()
          Returns the compressed size of the data written.
 int getSize()
          Returns the real (uncompressed) size of data written.
 boolean isOpen()
          Returns true if buffer is not null
 int readBytes(byte[] buffer, int start, int count)
          transfers count bytes from class buffer to buf.
 void readFully(Stream inputStream, int retryCount, int bufSize)
          Reads all data from the input stream into our buffer.
 java.lang.String readLine()
          Returns a line of text read from internal buffer or null if no more lines left.
 void setMode(int newMode)
          Changes the mode to READ_MODE or WRITE_MODE.
 int skipBytes(int count)
          NOT IMPLEMENTED.
 int writeBytes(byte[] buffer, int start, int count)
          this writes to the byte array, expanding it if necessary. returns the number of bytes written.
 void writeLine(java.lang.String s)
          Writes a line of text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

READ_MODE

public static final int READ_MODE
Used in the setMode method. Turns the mode into READ.

WRITE_MODE

public static final int WRITE_MODE
Used in the setMode method. Turns the mode into WRITE.

DESTRUCTIVE_READ_MODE

public static final int DESTRUCTIVE_READ_MODE
used in the setMode method. Turns the mode into READ, and after reading each buffer, discards it, releasing memory. The BBAS will not be able to read the buffer again.

crlf

public static byte[] crlf
Defines the line terminator, which is by default \r\n. To change it to \n only use BigByteArrayStream.crlf = new byte[]{'\n'};
Constructor Detail

BigByteArrayStream

public BigByteArrayStream(int compressionLevel)
Creates a new BigByteArrayStream, using the given compression level (0 = no compression, 9 = max compression).
Method Detail

flush

public void flush()
Commits the last part of the file that didn't achieved the limits. It also calls
setMode(READ_MODE)
so that the buffer can be read.

setMode

public void setMode(int newMode)
Changes the mode to READ_MODE or WRITE_MODE. The flush method is called.

close

public boolean close()
Deletes all internal buffers. Do not try to use this class afterwards.
Overrides:
close in class Stream

getSize

public int getSize()
Returns the real (uncompressed) size of data written.

getCompressedSize

public int getCompressedSize()
Returns the compressed size of the data written.

skipBytes

public int skipBytes(int count)
NOT IMPLEMENTED. Always returns -1

isOpen

public boolean isOpen()
Returns true if buffer is not null
Overrides:
isOpen in class Stream

readBytes

public int readBytes(byte[] buffer,
                     int start,
                     int count)
transfers count bytes from class buffer to buf.
Overrides:
readBytes in class Stream
Returns:
the number of bytes read.

writeBytes

public int writeBytes(byte[] buffer,
                      int start,
                      int count)
this writes to the byte array, expanding it if necessary. returns the number of bytes written.
Overrides:
writeBytes in class Stream
Since:
SuperWaba2_0beta2

readLine

public java.lang.String readLine()
Returns a line of text read from internal buffer or null if no more lines left.

readFully

public void readFully(Stream inputStream,
                      int retryCount,
                      int bufSize)
Reads all data from the input stream into our buffer. Note that setMode(WRITE) is called prior to writting. When returned, data is ready to be read.
Parameters:
inputStream - The input stream from where data will be read
retryCount - The number of times to retry if no data is read. In remote connections, use at least 5; for files, it can be 0.
bufSize - The size of buffer used to read data.
Since:
SuperWaba 5.7

writeLine

public void writeLine(java.lang.String s)
Writes a line of text. The \r\n line terminator is appended to the line. You can avoid this by setting BigByteArrayStream.crlf = new byte[0];