Refine search
public int encode(char[] chars, int offset, int len, byte[] bytes) { ce.reset(); ByteBuffer bb = ByteBuffer.wrap(bytes); CharBuffer cb = CharBuffer.wrap(chars, offset, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); return bb.position(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new RuntimeException("Encode error: " + x.getMessage(), x); } } }
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(StandardCharsets.US_ASCII)); os.write(VALUE_SEPARATOR); 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); } } }
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(); } } }
static ByteBuffer encodeString(CharBuffer src, Charset charset) { final CharsetEncoder encoder = CharsetUtil.getEncoder(charset); final ByteBuffer dst = ByteBuffer.allocate( (int) ((double) src.remaining() * encoder.maxBytesPerChar())); try { CoderResult cr = encoder.encode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } dst.flip(); return dst; }
private void advance() throws IOException { assert !endOfInput; assert !bbuf.hasRemaining() : "advance() should be called when output byte buffer is empty. bbuf: " + bbuf + ", as string: " + bbuf.asCharBuffer().toString(); assert cbuf.remaining() < 2; // given that bbuf.capacity = 3 x cbuf.capacity, the only time that we should have a // remaining char is if the last char read was the 1st half of a surrogate pair if (cbuf.remaining() == 0) { cbuf.clear(); } else { cbuf.compact(); } int n = reader.read(cbuf); // read #1 cbuf.flip(); CoderResult result; endOfInput = n == -1; bbuf.clear(); result = encoder.encode(cbuf, bbuf, endOfInput); checkEncodeResult(result); if (endOfInput) { result = encoder.flush(bbuf); checkEncodeResult(result); } bbuf.flip(); }
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); } }