Bytes and Chars plus encoding

Hi,

I have a requirement to read bytes and at times characters from a given input stream. The characters in the input stream may be encoded in a particular encoding. I have a definition file, for the input stream I am supposed to read, which tells when and how much bytes/chars is to be read. Java allows to read eihter bytes or characters from a given stream. How can I read both from a given input stream?

For example, I want something like:

InputStream myInputStream;

int b = myInputStream.readByte();

// the character read below should be decoded using the appropriate

// encoding, which is known from the definition file.

int c = myInputStream.readChar();

Thanks,

Rahul.

[752 byte] By [Rahul_Srivastava] at [2007-9-30 13:58:24]
# 1

You should be able to use the DataInputStream class to do what you want.

You would use the readByte() or read(byte[] b) or read(byte[] b, int off, int len) method when you wanted to read byte data.

You would use the readChar() method when you wanted to read char data.

There is very clear example code implementing this class and its methods here: http://java.sun.com/docs/books/tutorial/essential/io/dataIO.html

ChuckBing at 2007-7-4 23:57:30 > top of Java-index,Administration Tools,Sun Connection...
# 2
It should work to use InputStream to read bytes into a byte array. Then if you need to change the bytes to a specific encoding, use the constructor of String that takes a byte array and an encoding specification to convert the bytes to chars.
atmguy at 2007-7-4 23:57:30 > top of Java-index,Administration Tools,Sun Connection...