Refine search
/** * Fills the internal char buffer from the reader. * * @throws IOException * If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); final int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). final int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == EOF) { endOfInput = true; } else { encoderIn.position(position+c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuf = CharBuffer.allocate(decodedCapacity); charBuf.clear(); CoderResult result = decoder.reset().decode(byteBuf, charBuf, true); try { if (!result.isUnderflow()) { result.throwException(); result = decoder.flush(charBuf); if (!result.isUnderflow()) { result.throwException(); strBuf.append(charBuf.flip());
try { while (byteBuffer.hasRemaining()) { while (charBuffer.hasRemaining()) { final CoderResult result = encoder.encode(charBuffer, byteBuffer, false); if (result.isOverflow()) { return true; if (result.isUnderflow()) { filled = true; break; if (result.isError()) { if (result.isMalformed()) { throw msg.malformedInput(); if (result.isUnmappable()) { throw msg.unmappableCharacter(); charBuffer.compact(); try { final int cnt = reader.read(charBuffer); if (cnt == -1) { return filled; charBuffer.flip();
private void checkEncodeResult(CoderResult result) throws CharacterCodingException { if (result.isError()) { result.throwException(); } }
/** * Fills the byte output buffer from the input char buffer. * * @throws CharacterCodingException * an error encoding data */ private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); if (result.isError()) { result.throwException(); } this.bbuf.flip(); }
private static void decode(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) { try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } }
@Override public void write(OutputStream out) throws IOException { Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); CharBuffer charBuf = CharBuffer.allocate(BUFFER_SIZE); byte[] buf = new byte[BUFFER_SIZE * 2]; ByteBuffer byteBuf = ByteBuffer.wrap(buf); while (content.read(charBuf) != -1) { encoder.reset(); charBuf.flip(); byteBuf.clear(); CoderResult result = encoder.encode(charBuf, byteBuf, false); if (result.isError()) { throw new IOException(result.toString()); } byteBuf.flip(); out.write(buf, 0, byteBuf.limit()); charBuf.clear(); } out.flush(); } }
public ByteBuffer getBytes(String charsetName) { if (!isEnd) { throw new IllegalStateException("The conact has not been end."); } final CharsetEncoder encoder = Charset.forName(charsetName).newEncoder().onMalformedInput( CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); final CharBuffer cb = CharBuffer.wrap(this.value, 0, this.value.length); try { encoder.reset(); final int len = (int) (this.length * (double) encoder.maxBytesPerChar()); final ByteBuffer bb = ByteBuffer.allocate(len); CoderResult cr = encoder.encode(cb, bb, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(bb); if (!cr.isUnderflow()) { cr.throwException(); } return bb; } catch (Exception x) { throw new Error(x); } }
boolean utf16 = decoder.charset().name().startsWith("UTF-16"); decoder.reset(); int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1; CharBuffer out = CharBuffer.allocate(expectedLength); for (;;) { CoderResult cr; if (cr.isUnderflow()) { break; if (cr.isOverflow()) { CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength); out.flip(); o.put(out); if (cr.isError()) { cr.throwException();
if (_bb == null) { _bb = ByteBuffer.allocate(6); _cb = CharBuffer.allocate(1); _dc = Charset.forName("UTF-8").newDecoder(); } else { _bb.clear(); _cb.clear(); _bb.put((byte) b); _dc.reset(); _bb.flip(); CoderResult result = _dc.decode(_bb, _cb, true); _cb.flip(); if (result.isError() || !_cb.hasRemaining()) return REPLACEMENT;
byte[] byteArray = ...; String charsetName = ...; ByteBuffer buffer = ByteBuffer.wrap(byteArray, dstBegin, dstEnd - dstBegin); CharsetEncoder encoder = Charset.forName(charsetName).newEncoder(); CoderResult result = encoder.encode(CharBuffer.wrap(string), buffer, true); if (!result.isUnderflow()) { result.throwException(); }
private void fillByteBuffer(Reader reader) throws IOException { CharBuffer cbuf = CharBuffer.allocate(DEFAULT_CHAR_BUFFER_SIZE); ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE); List<byte[]> list = new ArrayList<byte[]>(); while (true) { cbuf.clear(); int size = reader.read(cbuf); if (size <= 0) { break; } cbuf.limit(cbuf.position()); cbuf.rewind(); boolean eof = false; while (!eof) { CoderResult cr = encoder.encode(cbuf, bbuf, eof); if (cr.isError()) { cr.throwException(); } else if (cr.isUnderflow()) { appendBytes(list, bbuf); eof = true; } else if (cr.isOverflow()) { appendBytes(list, bbuf); bbuf.clear(); } } } getByteArray(list); }
private Map<String, Object> buildStringKeyMap(ByteBuffer input) { int payloadSize = input.remaining(); if (chars == null || chars.remaining() < payloadSize) { chars = CharBuffer.allocate(payloadSize); } final CoderResult coderResult = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(input, chars, true); Map<String, Object> theMap; if (coderResult.isUnderflow()) { chars.flip(); try { theMap = parseString(chars.toString()); } finally { chars.clear(); } } else { throw new ParseException("Failed with CoderResult[%s]", coderResult); } return theMap; }
final CharsetEncoder enc = this.charset.newEncoder(); enc.onMalformedInput(CodingErrorAction.REPORT); enc.onUnmappableCharacter(CodingErrorAction.REPORT); final CharBuffer cb = CharBuffer.wrap(name); ByteBuffer out = ByteBuffer.allocate(name.length() + (name.length() + 1) / 2); while (cb.remaining() > 0) { final CoderResult res = enc.encode(cb, out, true); if (res.isUnmappable() || res.isMalformed()) { if (res.length() * 6 > out.remaining()) { out = ZipEncodingHelper.growBuffer(out, out.position() + res.length() * 6); for (int i = 0; i < res.length(); ++i) { ZipEncodingHelper.appendSurrogate(out, cb.get()); } else if (res.isOverflow()) { } else if (res.isUnderflow()) { enc.flush(out);
Assert.notNull(charset, "Charset must not be null"); if (charSequence.length() != 0) { CharsetEncoder charsetEncoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer inBuffer = CharBuffer.wrap(charSequence); int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); ByteBuffer outBuffer = ensureCapacity(estimatedSize) .asByteBuffer(writePosition(), writableByteCount()); while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); if (cr.isUnderflow()) { break; if (cr.isOverflow()) { writePosition(outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar());
private String decode() { if (null == decoder) { decoder = UTF8.newDecoder(); } snapshot(); try { if (null == chars || chars.remaining() < buffer.remaining()) { chars = CharBuffer.allocate(buffer.remaining()); } else { chars.rewind(); } decoder.reset(); CoderResult cr = decoder.decode(buffer, chars, true); if (cr.isUnderflow()) { decoder.flush(chars); } chars.flip(); return chars.toString(); } finally { reset(); } }
boolean utf16 = encoder.charset().name().startsWith("UTF-16"); CharBuffer in = CharBuffer.wrap(val); limit(end); encoder.reset(); if (in.hasRemaining()) { cr = encoder.encode(in, buf(), true); } else { cr = encoder.flush(buf()); if (cr.isUnderflow() || cr.isOverflow()) { break; cr.throwException();
private void drainEncoder() throws IOException { // Strictly speaking, I think it's part of the CharsetEncoder contract that you call // encode with endOfInput true before flushing. Our ICU-based implementations don't // actually need this, and you'd hope that any reasonable implementation wouldn't either. // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway! CharBuffer chars = CharBuffer.allocate(0); while (true) { CoderResult result = encoder.encode(chars, bytes, true); if (result.isError()) { result.throwException(); } else if (result.isOverflow()) { flushBytes(false); continue; } break; } // Some encoders (such as ISO-2022-JP) have stuff to write out after all the // characters (such as shifting back into a default state). In our implementation, // this is actually the first time ICU is told that we've run out of input. CoderResult result = encoder.flush(bytes); while (!result.isUnderflow()) { if (result.isOverflow()) { flushBytes(false); result = encoder.flush(bytes); } else { result.throwException(); } } }
private void convert(byte[] buffer, Charset charset) throws IOException { CharBuffer decode = charset.decode(ByteBuffer.wrap(buffer)); CharsetDecoder decoder = charset.newDecoder(); ByteBuffer bb = ByteBuffer.wrap(buffer); CharBuffer cb = CharBuffer.allocate(buffer.length * 4); CoderResult result = decoder.decode(bb, cb, true); if (!result.isError()) { String s = new String(cb.array(), 0, cb.position()); s = doBackslashEncoding(s); super.load(new StringReader(s)); return; } throw new CharacterCodingException(); }
CharBuffer out = CharBuffer.wrap(buffer, offset, count); CoderResult result = CoderResult.UNDERFLOW; while (out.hasRemaining()) { if (in.available() == 0 && out.position() > offset) { result = decoder.decode(bytes, out, false); if (result.isUnderflow()) { result = decoder.decode(bytes, out, true); decoder.flush(out); decoder.reset(); if (result.isMalformed() || result.isUnmappable()) { result.throwException();