Java Code Examples for java.nio.charset.CoderResult#isOverflow()
The following examples show how to use
java.nio.charset.CoderResult#isOverflow() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlUtils.java From JobSchedulerCompat with MIT License | 6 votes |
public void flush() throws IOException { if (pos > 0) { if (out != null) { CharBuffer charBuffer = CharBuffer.wrap(text, 0, pos); CoderResult result = charset.encode(charBuffer, bytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result = charset.encode(charBuffer, bytes, true); continue; } break; } flushBytes(); out.flush(); } else { writer.write(text, 0, pos); writer.flush(); } pos = 0; } }
Example 2
Source File: CharSequenceWrapper.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Decode the buffer using the CharsetDecoder. * @param byteBuf * @return false if failed because the charbuffer was not big enough * @throws RuntimeException if it fails for encoding errors */ private boolean decodeUT8(ByteBuffer byteBuf) { // We give it all of the input data in call. boolean endOfInput = true; decoder.reset(); charBuffer.rewind(); // Convert utf-8 bytes to sequence of chars CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput); if (result.isOverflow()) { // Not enough space in the charBuffer. return false; } else if (result.isError()) { // Any other error try { result.throwException(); } catch (CharacterCodingException e) { throw new RuntimeException(e); } } return true; }
Example 3
Source File: WriterOutputStream.java From domino-jna with Apache License 2.0 | 6 votes |
/** * Decode the contents of the input ByteBuffer into a CharBuffer. * * @param endOfInput indicates end of input * @throws IOException if an I/O error occurs */ private void processInput(final boolean endOfInput) throws IOException { // Prepare decoderIn for reading decoderIn.flip(); CoderResult coderResult; while (true) { coderResult = decoder.decode(decoderIn, decoderOut, endOfInput); if (coderResult.isOverflow()) { flushOutput(); } else if (coderResult.isUnderflow()) { break; } else { // The decoder is configured to replace malformed input and unmappable characters, // so we should not get here. throw new IOException("Unexpected coder result"); } } // Discard the bytes that have been read decoderIn.compact(); }
Example 4
Source File: WriterOutputStream.java From aion-germany with GNU General Public License v3.0 | 6 votes |
/** * Decode the contents of the input ByteBuffer into a CharBuffer. * * @param endOfInput indicates end of input * @throws IOException if an I/O error occurs */ private void processInput(boolean endOfInput) throws IOException { // Prepare decoderIn for reading decoderIn.flip(); CoderResult coderResult; while (true) { coderResult = decoder.decode(decoderIn, decoderOut, endOfInput); if (coderResult.isOverflow()) { flushOutput(); } else if (coderResult.isUnderflow()) { break; } else { // The decoder is configured to replace malformed input and unmappable characters, // so we should not get here. throw new IOException("Unexpected coder result"); } } // Discard the bytes that have been read decoderIn.compact(); }
Example 5
Source File: HtmlDataObject.java From netbeans with Apache License 2.0 | 6 votes |
private CoderResult flushHead (CharBuffer in , ByteBuffer out) { buffer.flip(); CoderResult r = encoder.encode(buffer,out, in==null); if (r.isOverflow()) { remainder = buffer; buffer = null; return r; } else { buffer = null; if (in == null) { return r; } return encoder.encode(in, out, false); } }
Example 6
Source File: BinaryTruncator.java From parquet-mr with Apache License 2.0 | 6 votes |
Validity checkValidity(ByteBuffer buffer) { int pos = buffer.position(); CoderResult result = CoderResult.OVERFLOW; while (result.isOverflow()) { dummyBuffer.clear(); result = decoder.decode(buffer, dummyBuffer, true); } buffer.position(pos); if (result.isUnderflow()) { return Validity.VALID; } else if (result.isMalformed()) { return Validity.MALFORMED; } else { return Validity.UNMAPPABLE; } }
Example 7
Source File: CharSequenceWrapper.java From Bats with Apache License 2.0 | 6 votes |
/** * Decode the buffer using the CharsetDecoder. * @param byteBuf * @return false if failed because the charbuffer was not big enough * @throws RuntimeException if it fails for encoding errors */ private boolean decodeUT8(ByteBuffer byteBuf) { // We give it all of the input data in call. boolean endOfInput = true; decoder.reset(); charBuffer.rewind(); // Convert utf-8 bytes to sequence of chars CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput); if (result.isOverflow()) { // Not enough space in the charBuffer. return false; } else if (result.isError()) { // Any other error try { result.throwException(); } catch (CharacterCodingException e) { throw new RuntimeException(e); } } return true; }
Example 8
Source File: FastXmlSerializer.java From ticdesign with Apache License 2.0 | 6 votes |
public void flush() throws IOException { //Log.i("PackageManager", "flush mPos=" + mPos); if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos); CoderResult result = mCharset.encode(charBuffer, mBytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result = mCharset.encode(charBuffer, mBytes, true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText, 0, mPos); mWriter.flush(); } mPos = 0; } }
Example 9
Source File: TextEncoderHelper.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * If the CoderResult indicates the ByteBuffer is full, synchronize on the destination and write the content * of the ByteBuffer to the destination. If the specified ByteBuffer is owned by the destination, we have * reached the end of a MappedBuffer and we call drain() on the destination to remap(). * <p> * If the CoderResult indicates more can be encoded, this method does nothing and returns the temp ByteBuffer. * </p> * * @param destination the destination to write bytes to * @param temp the ByteBuffer containing the encoded bytes. May be a temporary buffer or may be the ByteBuffer of * the ByteBufferDestination * @param result the CoderResult from the CharsetEncoder * @return the ByteBuffer to encode into for the remainder of the text */ private static ByteBuffer drainIfByteBufferFull(final ByteBufferDestination destination, final ByteBuffer temp, final CoderResult result) { if (result.isOverflow()) { // byte buffer full // all callers already synchronize on destination but for safety ensure we are synchronized because // below calls to drain() may cause destination to swap in a new ByteBuffer object synchronized (destination) { final ByteBuffer destinationBuffer = destination.getByteBuffer(); if (destinationBuffer != temp) { temp.flip(); ByteBufferDestinationHelper.writeToUnsynchronized(temp, destination); temp.clear(); return destination.getByteBuffer(); } else { return destination.drain(destinationBuffer); } } } else { return temp; } }
Example 10
Source File: FastXmlSerializer.java From Android-PreferencesManager with Apache License 2.0 | 6 votes |
public void flush() throws IOException { // Log.i("PackageManager", "flush mPos=" + mPos); if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos); CoderResult result = mCharset.encode(charBuffer, mBytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result = mCharset.encode(charBuffer, mBytes, true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText, 0, mPos); mWriter.flush(); } mPos = 0; } }
Example 11
Source File: FastXmlSerializer.java From PreferenceFragment with Apache License 2.0 | 6 votes |
public void flush() throws IOException { //Log.i("PackageManager", "flush mPos=" + mPos); if (mPos > 0) { if (mOutputStream != null) { CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos); CoderResult result = mCharset.encode(charBuffer, mBytes, true); while (true) { if (result.isError()) { throw new IOException(result.toString()); } else if (result.isOverflow()) { flushBytes(); result = mCharset.encode(charBuffer, mBytes, true); continue; } break; } flushBytes(); mOutputStream.flush(); } else { mWriter.write(mText, 0, mPos); mWriter.flush(); } mPos = 0; } }
Example 12
Source File: ImapRequestLineReader.java From james-project with Apache License 2.0 | 5 votes |
private void flush() throws DecodingException { final CoderResult coderResult = decoder.flush(charBuffer); if (coderResult.isOverflow()) { upsizeCharBuffer(); flush(); } else if (coderResult.isError()) { throw new DecodingException(HumanReadableText.BAD_IO_ENCODING, "Bad character encoding"); } }
Example 13
Source File: OutputStreamWriter.java From jtransc with Apache License 2.0 | 5 votes |
private void convert(CharBuffer chars) throws IOException { while (true) { CoderResult result = encoder.encode(chars, bytes, false); if (result.isOverflow()) { // Make room and try again. flushBytes(false); continue; } else if (result.isError()) { result.throwException(); } break; } }
Example 14
Source File: MessageBuilderFactory.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
/** * Left in for reference. Less efficient, but potentially catches more errors. Behavior is * largely dependent on how strict the JVM's utf8 decoder is. It is possible on some JVMs to * decode a string that then throws an error when attempting to return it to bytes. * * @param bytes Bytes representing a utf8 string * @return The decoded string */ private String decodeStringStreaming(byte[] bytes) { try { ByteBuffer input = getBuffer(bytes); int bufSize = (int) (input.remaining() * localDecoder.get().averageCharsPerByte()); CharBuffer output = CharBuffer.allocate(bufSize); for (; ; ) { CoderResult result = localDecoder.get().decode(input, output, false); if (result.isError()) { return null; } if (result.isUnderflow()) { break; } if (result.isOverflow()) { // We need more room in our output buffer bufSize = 2 * bufSize + 1; CharBuffer o = CharBuffer.allocate(bufSize); output.flip(); o.put(output); output = o; } } if (input.remaining() > 0) { carryOver = input; } // Re-encode to work around bugs in UTF-8 decoder CharBuffer test = CharBuffer.wrap(output); localEncoder.get().encode(test); output.flip(); String text = output.toString(); return text; } catch (CharacterCodingException e) { return null; } }
Example 15
Source File: WriterOutputStream.java From marathonv5 with Apache License 2.0 | 5 votes |
private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException { decoder.reset(); chars.clear(); CoderResult result = decoder.decode(bytes, chars, true); if (result.isError() || result.isOverflow()) { throw new IOException(result.toString()); } else if (result.isUnderflow()) { chars.flip(); } }
Example 16
Source File: NTGVisitorBase.java From sailfish-core with Apache License 2.0 | 5 votes |
static byte[] encodeString(String str, int length) { byte[] array = new byte[length]; Arrays.fill(array, (byte)0x20); ByteBuffer buffer = ByteBuffer.wrap(array); CharBuffer charBuffer = CharBuffer.wrap(str); CoderResult result = ENCODER.get().encode(charBuffer, buffer, true); if (result.isOverflow()) { throw new EPSCommonException("The length of value = [" + str + "] is greater than length specified in the dictionary."); } return array; }
Example 17
Source File: ServletPrintWriter.java From lams with GNU General Public License v2.0 | 4 votes |
public void close() { if (outputStream.getServletRequestContext().getOriginalRequest().getDispatcherType() == DispatcherType.INCLUDE) { return; } if (closed) { return; } closed = true; try { boolean done = false; CharBuffer buffer; if (underflow == null) { buffer = CharBuffer.wrap(EMPTY_CHAR); } else { buffer = CharBuffer.wrap(underflow); underflow = null; } if (charsetEncoder != null) { do { ByteBuffer out = outputStream.underlyingBuffer(); if (out == null) { //servlet output stream has already been closed error = true; return; } CoderResult result = charsetEncoder.encode(buffer, out, true); if (result.isOverflow()) { outputStream.flushInternal(); if (out.remaining() == 0) { outputStream.close(); error = true; return; } } else { done = true; } } while (!done); } outputStream.close(); } catch (IOException e) { error = true; } }
Example 18
Source File: CharSequenceCustomEncodingBytesWriter.java From Chronicle-Map with Apache License 2.0 | 4 votes |
@Override public void write(Bytes out, @NotNull CharSequence cs) { // Write the actual cs length for accurate StringBuilder.ensureCapacity() while reading out.writeStopBit(cs.length()); long encodedSizePos = out.writePosition(); out.writeSkip(4); charsetEncoder.reset(); inputBuffer.clear(); outputBuffer.clear(); int csPos = 0; boolean endOfInput = false; // this loop inspired by the CharsetEncoder.encode(CharBuffer) implementation while (true) { if (!endOfInput) { int nextCsPos = Math.min(csPos + inputBuffer.remaining(), cs.length()); append(inputBuffer, cs, csPos, nextCsPos); inputBuffer.flip(); endOfInput = nextCsPos == cs.length(); csPos = nextCsPos; } CoderResult cr = inputBuffer.hasRemaining() ? charsetEncoder.encode(inputBuffer, outputBuffer, endOfInput) : CoderResult.UNDERFLOW; if (cr.isUnderflow() && endOfInput) cr = charsetEncoder.flush(outputBuffer); if (cr.isUnderflow()) { if (endOfInput) { break; } else { inputBuffer.compact(); continue; } } if (cr.isOverflow()) { outputBuffer.flip(); writeOutputBuffer(out); outputBuffer.clear(); continue; } try { cr.throwException(); } catch (CharacterCodingException e) { throw new IORuntimeException(e); } } outputBuffer.flip(); writeOutputBuffer(out); out.writeInt(encodedSizePos, (int) (out.writePosition() - encodedSizePos - 4)); }
Example 19
Source File: BinaryDecoder.java From termd with Apache License 2.0 | 4 votes |
public void write(byte[] data, int start, int len) { // Fill the byte buffer int remaining = bBuf.remaining(); if (len > remaining) { // Allocate a new buffer ByteBuffer tmp = bBuf; int length = tmp.position() + len; bBuf = ByteBuffer.allocate(length); tmp.flip(); bBuf.put(tmp); } bBuf.put(data, start, len); bBuf.flip(); // Drain the byte buffer while (true) { IntBuffer iBuf = IntBuffer.allocate(bBuf.remaining()); CoderResult result = decoder.decode(bBuf, cBuf, false); cBuf.flip(); while (cBuf.hasRemaining()) { char c = cBuf.get(); if (Character.isSurrogate(c)) { if (Character.isHighSurrogate(c)) { if (cBuf.hasRemaining()) { char low = cBuf.get(); if (Character.isLowSurrogate(low)) { int codePoint = Character.toCodePoint(c, low); if (Character.isValidCodePoint(codePoint)) { iBuf.put(codePoint); } else { throw new UnsupportedOperationException("Handle me gracefully"); } } else { throw new UnsupportedOperationException("Handle me gracefully"); } } else { throw new UnsupportedOperationException("Handle me gracefully"); } } else { throw new UnsupportedOperationException("Handle me gracefully"); } } else { iBuf.put((int) c); } } iBuf.flip(); int[] codePoints = new int[iBuf.limit()]; iBuf.get(codePoints); onChar.accept(codePoints); cBuf.compact(); if (result.isOverflow()) { // We still have work to do } else if (result.isUnderflow()) { if (bBuf.hasRemaining()) { // We need more input Helper.noop(); } else { // We are done Helper.noop(); } break; } else { throw new UnsupportedOperationException("Handle me gracefully"); } } bBuf.compact(); }
Example 20
Source File: AbstractIoBuffer.java From neoscada with Eclipse Public License 1.0 | 4 votes |
/** * {@inheritDoc} */ @Override public String getString(CharsetDecoder decoder) throws CharacterCodingException { if (!hasRemaining()) { return ""; } boolean utf16 = decoder.charset().name().startsWith("UTF-16"); int oldPos = position(); int oldLimit = limit(); int end = -1; int newPos; if (!utf16) { end = indexOf((byte) 0x00); if (end < 0) { newPos = end = oldLimit; } else { newPos = end + 1; } } else { int i = oldPos; for (;;) { boolean wasZero = get(i) == 0; i++; if (i >= oldLimit) { break; } if (get(i) != 0) { i++; if (i >= oldLimit) { break; } continue; } if (wasZero) { end = i - 1; break; } } if (end < 0) { newPos = end = oldPos + (oldLimit - oldPos & 0xFFFFFFFE); } else { if (end + 2 <= oldLimit) { newPos = end + 2; } else { newPos = end; } } } if (oldPos == end) { position(newPos); return ""; } limit(end); decoder.reset(); int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1; CharBuffer out = CharBuffer.allocate(expectedLength); for (;;) { CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); } if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength); out.flip(); o.put(out); out = o; continue; } if (cr.isError()) { // Revert the buffer back to the previous state. limit(oldLimit); position(oldPos); cr.throwException(); } } limit(oldLimit); position(newPos); return out.flip().toString(); }