List of usage examples for java.nio.charset CoderResult UNDERFLOW
CoderResult UNDERFLOW
To view the source code for java.nio.charset CoderResult UNDERFLOW.
Click Source Link
From source file:Base64Encoder.java
/** * Flushes this encoder.//from w w w . j a v 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: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 w w .j a v a 2s . 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
/** * 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./*www . j ava 2 s .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:android.framework.util.jar.Manifest.java
private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException { String nameString = name.getName(); os.write(nameString.getBytes(Charsets.US_ASCII)); os.write(VALUE_SEPARATOR);/* www . ja v a 2 s. c o m*/ encoder.reset(); bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2); CharBuffer cBuf = CharBuffer.wrap(value); while (true) { CoderResult r = encoder.encode(cBuf, bBuf, true); if (CoderResult.UNDERFLOW == r) { r = encoder.flush(bBuf); } os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position()); os.write(LINE_SEPARATOR); if (CoderResult.UNDERFLOW == r) { break; } os.write(' '); bBuf.clear().limit(LINE_LENGTH_LIMIT - 1); } }
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 a2s . c om*/ 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.apache.tika.parser.rtf.TextExtractor.java
private void pushBytes() throws IOException, SAXException, TikaException { if (pendingByteCount > 0 && (!groupState.ignore || nextMetaData != null)) { final CharsetDecoder decoder = getDecoder(); pendingByteBuffer.limit(pendingByteCount); assert pendingByteBuffer.position() == 0; assert outputBuffer.position() == 0; while (true) { // We pass true for endOfInput because, when // we are called, we should have seen a // complete sequence of characters for this // charset: final CoderResult result = decoder.decode(pendingByteBuffer, outputBuffer, true); final int pos = outputBuffer.position(); if (pos > 0) { if (inHeader || fieldState == 1) { pendingBuffer.append(outputArray, 0, pos); } else { lazyStartParagraph(); out.characters(outputArray, 0, pos); }/*from ww w . ja v a 2 s. co m*/ outputBuffer.position(0); } if (result == CoderResult.UNDERFLOW) { break; } } while (true) { final CoderResult result = decoder.flush(outputBuffer); final int pos = outputBuffer.position(); if (pos > 0) { if (inHeader || fieldState == 1) { pendingBuffer.append(outputArray, 0, pos); } else { lazyStartParagraph(); out.characters(outputArray, 0, pos); } outputBuffer.position(0); } if (result == CoderResult.UNDERFLOW) { break; } } // Reset for next decode decoder.reset(); pendingByteBuffer.position(0); } pendingByteCount = 0; }