FileDocCategorySizeDatePackage
IOUtil.javaAPI DocAndroid 1.5 API8680Wed May 06 22:41:04 BST 2009org.apache.harmony.nio.internal

IOUtil

public final class IOUtil extends Object

Fields Summary
private static final int
DEFAULT_BUFFER_SIZE
Constructors Summary
private IOUtil()


    // -------------------------------------------------------------------
    // Constructor
    // -------------------------------------------------------------------

    /*
     * No instance.
     */
      
        super();
    
Methods Summary
private static voidconvert(java.lang.Object lock, java.nio.charset.CharsetEncoder encoder, java.nio.ByteBuffer bytes, java.nio.CharBuffer chars, java.io.OutputStream out)

        synchronized (lock) {
            if (encoder == null) {
                // nio.07=Writer is closed.
                throw new IOException(Messages.getString("nio.07")); //$NON-NLS-1$
            }
            CoderResult result = encoder.encode(chars, bytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    // flush the output buffer
                    flushOutputStreamWriter(out, bytes, encoder, lock);
                    result = encoder.encode(chars, bytes, true);
                    continue;
                }
                break;
            }
        }
    
private static voidfillBuf(java.io.InputStream in, java.nio.ByteBuffer bytes, java.nio.CharBuffer chars, java.nio.charset.CharsetDecoder decoder)

        chars.clear();
        int read = 0;
        try {
            read = in.read(bytes.array());
        } catch (IOException e) {
            chars.limit(0);
            throw e;
        }
        if (read == -1) {
            chars.limit(0);
            return;
        }
        bytes.limit(read);
        boolean endOfInput = read < DEFAULT_BUFFER_SIZE;
        CoderResult result = decoder.decode(bytes, chars, endOfInput);
        if (result.isError()) {
            throw new IOException(result.toString());
        }
        bytes.clear();
        chars.flip();
    
public static voidflushOutputStreamWriter(java.io.OutputStream out, java.nio.ByteBuffer bytes, java.nio.charset.CharsetEncoder encoder, java.lang.Object lock)

        synchronized (lock) {
            if (encoder == null) {
                // nio.07=Writer is closed.
                throw new IOException(Messages.getString("nio.07")); //$NON-NLS-1$
            }
            int position;
            if ((position = bytes.position()) > 0) {
                bytes.flip();
                out.write(bytes.array(), 0, position);
                bytes.clear();
            }
            out.flush();
        }
    
public static intreadInputStreamReader(java.io.InputStream in, java.nio.ByteBuffer bytes, java.nio.CharBuffer chars, java.nio.charset.CharsetDecoder decoder, java.lang.Object lock)

        synchronized (lock) {
            if (in != null) {
                if (chars.limit() == chars.position()) {
                    fillBuf(in, bytes, chars, decoder);
                }
                if (chars.limit() == 0) {
                    return -1;
                }
                return chars.get();
            }
            // nio.06=InputStreamReader is closed.
            throw new IOException(Messages.getString("nio.06")); //$NON-NLS-1$
        }
    
public static intreadInputStreamReader(char[] buf, int offset, int length, java.io.InputStream in, java.nio.ByteBuffer bytes, java.nio.CharBuffer chars, java.nio.charset.CharsetDecoder decoder, java.lang.Object lock)

        synchronized (lock) {
            if (in != null) {
                if (length == 0) {
                    return 0;
                }
                Util.assertArrayIndex(buf, offset, length);
                // read at least once
                if (chars.limit() == chars.position()) {
                    fillBuf(in, bytes, chars, decoder);
                }
                int position = chars.position();
                int availableChars = chars.limit() - position;
                // read at least once for one byte
                int needChars = length;
                while (availableChars < needChars) {
                    System.arraycopy(chars.array(), position, buf, offset,
                            availableChars);
                    chars.position(position + availableChars);
                    needChars -= availableChars;
                    offset += availableChars;
                    if (in.available() <= 0) {
                        return needChars == length ? -1 : length - needChars;
                    }
                    fillBuf(in, bytes, chars, decoder);
                    position = chars.position();
                    availableChars = chars.limit();
                    if (availableChars == 0) {
                        return needChars == length ? -1 : length - needChars;
                    }
                }
                System.arraycopy(chars.array(), position, buf, offset,
                        needChars);
                chars.position(chars.position() + needChars);
                return length;
            }
            // nio.06=InputStreamReader is closed.
            throw new IOException(Messages.getString("nio.06")); //$NON-NLS-1$
        }
    
public static voidwriteOutputStreamWriter(java.lang.String str, int offset, int count, java.io.OutputStream out, java.nio.ByteBuffer bytes, java.nio.charset.CharsetEncoder encoder, java.lang.Object lock)

        Util.assertArrayIndex(str.length(), offset, count);
        CharBuffer chars = CharBuffer.wrap(str, offset, count + offset);
        convert(lock, encoder, bytes, chars, out);
    
public static voidwriteOutputStreamWriter(int oneChar, java.io.OutputStream out, java.nio.ByteBuffer bytes, java.nio.charset.CharsetEncoder encoder, java.lang.Object lock)

        synchronized (lock) {
            if (encoder == null) {
                // nio.07=Writer is closed.
                throw new IOException(Messages.getString("nio.07")); //$NON-NLS-1$
            }
            CharBuffer chars = CharBuffer.wrap(new char[] { (char) oneChar });
            convert(lock, encoder, bytes, chars, out);
        }
    
public static voidwriteOutputStreamWriter(char[] buf, int offset, int count, java.io.OutputStream out, java.nio.ByteBuffer bytes, java.nio.charset.CharsetEncoder encoder, java.lang.Object lock)

        Util.assertArrayIndex(buf, offset, count);
        CharBuffer chars = CharBuffer.wrap(buf, offset, count);
        convert(lock, encoder, bytes, chars, out);