FileDocCategorySizeDatePackage
MsqlInputStream.javaAPI DocExample2325Tue Jan 01 00:00:00 GMT 1980COM.imaginary.sql.msql

MsqlInputStream.java

/* Copyright (c) 1997 George Reese */
package COM.imaginary.sql.msql;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;

/**
 * The MsqlInputStream class handles communication between the mSQL database
 * and the JDBC implementation.<BR>
 * Last modified 97/04/19
 * @version @(#) MsqlInputStream.java 1.3@(#)
 * @author George Reese (borg@imaginary.com)
 */
public class MsqlInputStream {
    private DataInputStream input;

    /**
     * Constructs an MsqlInputStream using an InputStream from
     * a socket connecting the driver to the database.
     * @param in the InputStream representing data form the database
     * @exception java.io.IOException an error occurred creating
     * a DataInputStream for the InputStream
     */
    public MsqlInputStream(InputStream in) throws IOException {
	super();
	input = new DataInputStream(new BufferedInputStream(in));
    }

    /**
     * Closes the stream.
     * @exception java.io.IOException an error occurred closing the stream
     */
    public void close() throws IOException {
	input.close();
    }

    /**
     * Reads data from the mSQL database as an array of bytes.
     * @return data from the database as an array of bytes
     * @exception java.io.IOException an error occured on read
     */
    public byte[] read() throws IOException {
	byte[] bytes;
	int size = 0;

	for(int i=0; i<4; i++) {
	    int b = input.read();
	    
	    if( i > 0 ) {
		size += b<<(i*8);
	    }
	    else {
		size += b;
	    }
	}
	bytes = new byte[size];
	input.readFully(bytes);
	return bytes;
    }

    /**
     * Instead of reading data raw, you can read it as a string using
     * the ASCII character set.
     * @return the string from the database
     * @exception java.io.IOException an error occurred on read
     */
    public String readAsciiString() throws IOException {
	return new String(read(), "8859_9");
    }

    /**
     * Instead of reading data raw, you can read it using the Unicode
     * character set.  Right now this is likely to give you nonsense.
     * @return a string from the database
     * @exception java.io.IOException an error occurred on read
     */
    public String readUnicodeString() throws IOException {
	return new String(read(), "Unicode");
    }
}