Example usage for java.nio.charset CoderResult OVERFLOW

List of usage examples for java.nio.charset CoderResult OVERFLOW

Introduction

In this page you can find the example usage for java.nio.charset CoderResult OVERFLOW.

Prototype

CoderResult OVERFLOW

To view the source code for java.nio.charset CoderResult OVERFLOW.

Click Source Link

Document

Result object indicating overflow, meaning that there is insufficient room in the output buffer.

Usage

From source file:Main.java

private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();/*from   w  ww . jav a2  s.  com*/
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}

From source file:Base64Encoder.java

/**
 * Flushes this encoder./*from w  w  w.j  av  a  2 s . c o m*/
 *
 * <p> The default implementation of this method does nothing, and always
 * returns {@link CoderResult#UNDERFLOW}.  This method should be overridden
 * by encoders that may need to write final bytes to the output buffer
 * once the entire input sequence has been read. </p>
 *
 * @param  out
 *         The output byte buffer
 *
 * @return  A coder-result object, either {@link CoderResult#UNDERFLOW} or
 *          {@link CoderResult#OVERFLOW}
 */
protected java.nio.charset.CoderResult implFlush(java.nio.ByteBuffer out) {
    if (encState != 0 && encState != 4)
        throw new IllegalArgumentException("Base-64 text ends prematurely");
    if (excessByte == null) {
        implReset();
        return CoderResult.UNDERFLOW;
    }
    if (out.remaining() > 0) {
        out.put(excessByte.byteValue());
        implReset();
        return CoderResult.UNDERFLOW;
    } else
        return CoderResult.OVERFLOW;

}

From source file:Base64Encoder.java

/**
 * Encodes one or more characters into one or more bytes.
 *
 * <p> This method encapsulates the basic encoding loop, encoding as many
 * characters as possible until it either runs out of input, runs out of room
 * in the output buffer, or encounters an encoding error.  This method is
 * invoked by the {@link #encode encode} method, which handles result
 * interpretation and error recovery./*from  w w w  .  j  av  a  2s. c  o  m*/
 *
 * <p> The buffers are read from, and written to, starting at their current
 * positions.  At most {@link Buffer#remaining in.remaining()} characters
 * will be read, and at most {@link Buffer#remaining out.remaining()}
 * bytes will be written.  The buffers' positions will be advanced to
 * reflect the characters read and the bytes written, but their marks and
 * limits will not be modified.
 *
 * <p> This method returns a {@link CoderResult} object to describe its
 * reason for termination, in the same manner as the {@link #encode encode}
 * method.  Most implementations of this method will handle encoding errors
 * by returning an appropriate result object for interpretation by the
 * {@link #encode encode} method.  An optimized implementation may instead
 * examine the relevant error action and implement that action itself.
 *
 * <p> An implementation of this method may perform arbitrary lookahead by
 * returning {@link CoderResult#UNDERFLOW} until it receives sufficient
 * input.  </p>
 *
 * @param  in
 *         The input character buffer
 *
 * @param  out
 *         The output byte buffer
 *
 * @return  A coder-result object describing the reason for termination
 */
public java.nio.charset.CoderResult encodeLoop(java.nio.CharBuffer in, java.nio.ByteBuffer out) {
    if (excessByte != null) {
        if (out.remaining() > 0) {
            out.put(excessByte.byteValue());
            excessByte = null;
        } else
            return CoderResult.OVERFLOW;

    }
    while (in.remaining() > 0) {
        char inch = in.get();
        int code = (int) inch >= encTable.length ? CHARCODE_INVALID : encTable[(int) inch];
        if (encState < 4) {
            switch (code) {
            case CHARCODE_INVALID:
                throw new IllegalArgumentException("Invalid base-64 character'" + inch + "'");
            case CHARCODE_WHITESPACE:
                break;
            case CHARCODE_PADDER:
                if (encState == 1)
                    throw new IllegalArgumentException("Mal-formed base-64 (= after one character");
                encState = 4;
                break;
            default:
                switch (encState) {
                case 0:
                    bits = code << 2;
                    encState = 1;
                    break;
                case 1:
                    encState = 2;
                    int v = bits | ((code >> 4) & 3);
                    bits = (code << 4) & 0xF0;
                    if (!out(out, v))
                        return CoderResult.OVERFLOW;
                    break;
                case 2:
                    encState = 3;
                    v = bits | (code >> 2) & 0x0f;
                    bits = (code << 6) & 0xC0;
                    if (!out(out, v))
                        return CoderResult.OVERFLOW;
                    break;
                case 3:
                    encState = 0;
                    bits |= (code & 0x3f);
                    if (!out(out, bits))
                        return CoderResult.OVERFLOW;
                    break;
                }

                break;
            }

        }
    }
    return CoderResult.UNDERFLOW;
}

From source file:org.apache.tika.parser.html.charsetdetector.charsets.XUserDefinedCharset.java

public CharsetDecoder newDecoder() {
    return new CharsetDecoder(this, 1, 1) {
        @Override/*from w  w  w . ja v  a 2s.  c o m*/
        protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
            while (true) {
                if (!in.hasRemaining())
                    return CoderResult.UNDERFLOW;
                if (!out.hasRemaining())
                    return CoderResult.OVERFLOW;
                byte b = in.get();
                out.append((char) ((b >= 0) ? b : 0xF700 + (b & 0xFF)));
            }
        }
    };
}

From source file:org.itstechupnorth.walrus.base.ArticleBuffer.java

public WritableByteChannel saveTo(WritableByteChannel out) throws IOException {
    saving = true;/*from  w  w  w .java 2s  . c  om*/
    final int originalPosition = buffer.position();
    // System.out.println(moniker() + "saving@" + buffer.position());
    try {
        buffer.flip();
        encoder.reset();

        outBuffer.clear();
        boolean more = true;
        while (more) {
            final CoderResult result = encoder.encode(buffer, outBuffer, true);
            outBuffer.flip();
            out.write(outBuffer);
            outBuffer.clear();
            more = CoderResult.OVERFLOW.equals(result);
        }
        return out;
    } finally {
        buffer.clear();
        buffer.position(originalPosition);
        // System.out.println(moniker() + "saved@" + buffer.position());
        saving = false;
    }
}

From source file:org.itstechupnorth.walrus.base.ArticleBuffer.java

public void saveTo(ZipArchiveOutputStream out) throws IOException {
    saving = true;/* ww  w . j a  v  a  2s  .  c o  m*/
    final int originalPosition = buffer.position();
    // System.out.println(moniker() + "saving@" + buffer.position());
    try {
        buffer.flip();
        encoder.reset();
        final String name = NAME_PREFIX + getTitleHash() + NAME_SUFFIX;
        final ZipArchiveEntry entry = new ZipArchiveEntry(name);
        entry.setComment(getTitle());
        out.putArchiveEntry(entry);

        outBuffer.clear();
        boolean more = true;
        while (more) {
            final CoderResult result = encoder.encode(buffer, outBuffer, true);
            out.write(outBuffer.array(), 0, outBuffer.position());
            outBuffer.clear();
            more = CoderResult.OVERFLOW.equals(result);
        }

        out.closeArchiveEntry();
    } finally {
        buffer.clear();
        buffer.position(originalPosition);
        // System.out.println(moniker() + "saved@" + buffer.position());
        saving = false;
    }
}