Gen_Readerpublic class Gen_Reader extends StreamReader Generic interface for stream conversion reading of
specific character encoded input streams.
. |
Fields Summary |
---|
private String | encSaved encoding string from construction. | private int | idNative handle for conversion routines. | private byte[] | bufLocal buffer to read converted characters. | private int | maxByteLenMaximum length of characters in local buffer. | private byte[] | tmpLocal buffer for conversion routines. | private int | posCurrent position in the temporary buffer. |
Constructors Summary |
---|
Gen_Reader(String enc)Constructor for generic reader.
id = Conv.getHandler(enc);
if (id == -1) {
// this lets Helper throw UnsupportedEncodingException
throw new ClassNotFoundException();
}
this.enc = enc;
maxByteLen = Conv.getMaxByteLength(id);
pos = maxByteLen;
tmp = new byte[pos];
buf = new byte[pos];
|
Methods Summary |
---|
private int | get()Get a byte from the temporary buffer or from the
input stream.
if (pos < tmp.length) {
return tmp[pos++] & 0xff;
}
return in.read();
| private void | init()reset the state
pos = maxByteLen;
| public java.io.Reader | open(java.io.InputStream in, java.lang.String enc)Generic routine to open an InputStream with a specific
character encoding.
if (!enc.equals(this.enc)) {
throw new UnsupportedEncodingException();
}
init();
return super.open(in, enc);
| private void | put(int b)Put a byte back into the temporary buffer.
if (pos == 0) {
throw new IOException();
}
tmp[--pos] = (byte) b;
| public synchronized int | read()Read a single converted character.
int c = get();
if (c == -1) {
return -1;
}
char[] cb = {(char)0xFFFD};
int bufLen = 0;
buf[bufLen++] = (byte) c;
boolean eof = false;
for (int i = 1; i < maxByteLen; i++) {
c = get();
if (c == -1) {
eof = true;
break;
}
buf[bufLen++] = (byte) c;
}
int bytelen = Conv.getByteLength(id, buf, 0, bufLen);
/* the byte indicates that there are tailing bytes */
if (bytelen == -1) {
/* if the stream is in the end, throw IOException */
if (eof) {
throw new IOException(/* "incomplete byte" */);
}
/* put the read ahead bytes back to the stream */
for (int i = bufLen; i > 1; i--) {
put(buf[--bufLen]);
}
/* return the leading byte as an unknown character */
return cb[0];
}
/* the byte is invalid */
if (bytelen == 0) {
for (int i = bufLen; i > 1; i--) {
put(buf[--bufLen]);
}
return cb[0];
}
/* put the read ahead bytes back to the stream */
if (bytelen < bufLen) {
for (int i = bufLen; i > bytelen; i--) {
put(buf[--bufLen]);
}
}
int convLen = Conv.byteToChar(id, buf, 0, bufLen, cb, 0, 1);
if (convLen != 1) {
throw new IOException(/*"Converter error"*/);
}
return cb[0];
| public synchronized int | read(char[] cbuf, int off, int len)Read a block of converted characters.
/* first, check that the stream has been reached to eof */
int c = get();
if (c == -1) {
return -1;
}
put(c); /* put it back */
int maxlen = len * maxByteLen;
if (buf.length < maxlen) {
buf = new byte[maxlen];
}
int bufLen = readNumOfChars(buf, len);
int ret = 0;
if (bufLen > 0) {
ret = Conv.byteToChar(id, buf, 0, bufLen, cbuf, off, len);
}
if (buf.length > maxByteLen) {
buf = new byte[maxByteLen];
}
return ret;
| private int | readNumOfChars(byte[] b, int num)Read a specific number of characters from the
input stream.
int charsRead = 0;
int bbLen = 0;
byte bb[];
if (b == null) {
bb = new byte[num * maxByteLen];
} else {
bb = b;
}
boolean eof = false;
while (charsRead < num) {
int c = get();
if (c == -1) {
break;
}
int offset = bbLen;
bb[bbLen++] = (byte) c;
for (int i = 1; i < maxByteLen; i++) {
c = get();
if (c == -1) {
eof = true;
break;
}
bb[bbLen++] = (byte) c;
}
int readNum = bbLen - offset;
int bytelen = Conv.getByteLength(id, bb, offset, readNum);
/* the byte indicates that there are tailing bytes */
if (bytelen == -1) {
/* save the leading byte for the next read */
if (eof) {
for (int i = readNum; i > 0; i--) {
put(bb[--bbLen]);
}
break;
}
/* put the rest of bytes back into the stream */
for (int i = readNum; i > 1; i--) {
put(bb[--bbLen]);
}
/* the byte is invalid */
} else if (bytelen == 0) {
for (int i = readNum; i > 1; i--) {
put(bb[--bbLen]);
}
} else if (bytelen < readNum) {
for (int i = readNum; i > bytelen; i--) {
put(bb[--bbLen]);
}
}
charsRead++;
}
if (b != null) {
return bbLen;
} else {
return charsRead;
}
| public void | reset()Reset the stream.
init();
| public int | sizeOf(byte[] c, int offset, int length)Get the size of the converted bytes as a Unicode
byte array.
return Conv.sizeOfByteInUnicode(id, c, offset, length);
| public long | skip(long n)Skip over a number of bytes in the input stream.
if (n < 0L) {
throw new IllegalArgumentException("skip value is negative");
}
/* see the stream has been reached to eof */
int c = get();
if (c == -1) {
return 0;
}
put(c);
return readNumOfChars(null, (int) n);
|
|