FileDocCategorySizeDatePackage
Gen_Writer.javaAPI DocJ2ME MIDP 2.04095Thu Nov 07 12:02:20 GMT 2002com.sun.cldc.i18n.j2me

Gen_Writer

public class Gen_Writer extends StreamWriter
Generic interface for stream conversion writing of specific character encoded input streams. .

Fields Summary
private String
enc
Saved encoding string from construction.
private int
id
Native handle for conversion routines.
private byte[]
buf
Local buffer to write converted characters.
private int
maxByteLen
Maximum length of characters in local buffer.
Constructors Summary
Gen_Writer(String enc)
Constructor for generic writer.

param
enc character encoding to use for byte to character conversion.
exception
ClassNotFoundException is thrown if the conversion class is not available

        id = Conv.getHandler(enc);
        if (id == -1) {
            throw new ClassNotFoundException();
        }
        this.enc = enc;
        maxByteLen = Conv.getMaxByteLength(id);
        buf = new byte[maxByteLen];
    
Methods Summary
public java.io.Writeropen(java.io.OutputStream out, java.lang.String enc)
Generic routine to open an OutputStream with a specific character encoding.

param
out the output stream to process
param
enc the character encoding for the output stream
return
Writer instance for converted characters
throws
UnsupportedEncodingException if encoding is not supported

        if (!enc.equals(this.enc)) {
            throw new UnsupportedEncodingException();
        }
        return super.open(out, enc);
    
public intsizeOf(char[] cbuf, int off, int len)
Get the size of the converted bytes as a Unicode byte array.

param
cbuf array of bytes to compute size
param
off offset in the provided buffer
param
len length of bytes to process
return
length of converted characters.

        return Conv.sizeOfUnicodeInByte(id, cbuf, off, len);
    
public synchronized voidwrite(int c)
Write a single converted character.

param
c the character to be output
exception
IOException is thrown if the output stream could not be written with the converted bytes

        char cbuf[] = {(char)c};

        int len = Conv.charToByte(id, cbuf, 0, 1, buf, 0, buf.length);

        if (len > 0) {
            out.write(buf, 0, len);
        }
    
public synchronized voidwrite(char[] cbuf, int off, int len)
Write a block of converted characters.

param
cbuf output buffer of characters to convert
param
off initial offset into the provided buffer
param
len length of characters in the buffer
exception
IOException is thrown if the output stream could not be written with the converted bytes

        int maxlen = len * maxByteLen;
        if (buf.length < maxlen) {
            buf = new byte[maxlen];
        }

        len = Conv.charToByte(id, cbuf, off, len, buf, 0, buf.length);

        if (len > 0) {
            out.write(buf, 0, len);
        }

        if (buf.length > maxByteLen) {
            buf = new byte[maxByteLen];
        }
    
public synchronized voidwrite(java.lang.String str, int off, int len)
Write a block of converted characters from a string.

param
str string to convert
param
off initial offset into the string
param
len length of characters in the string to process
exception
IOException is thrown if the output stream could not be written with the converted bytes

        for (int i = 0; i < len; i++) {
            write(str.charAt(off + i));
        }