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; } }
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char c = src.get(); if (c <= '\u00FF') { if (!dst.hasRemaining()) return CoderResult.OVERFLOW; dst.put((byte)c); mark++; continue; } if (sgp.parse(c, src) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
@Benchmark public byte[] byCharsetEncoder_US_ASCII() { try { CharsetEncoder encoder = asciiencode.get(); CharBuffer buffer = charbuffergenerator.get(); buffer.clear(); buffer.append(STR); buffer.flip(); ByteBuffer outbuffer = bytebuffergenerator.get(); outbuffer.clear(); CoderResult result = encoder.encode(buffer, outbuffer, false); if (result.isError()) { result.throwException(); } byte[] b = new byte[STR.length()]; outbuffer.flip(); outbuffer.get(b); return b; } catch (CharacterCodingException e) { throw new RuntimeException(e); } }
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { int b = src.get(); char c = decode(b); if (c == '\uFFFD') return CoderResult.unmappableForLength(1); if (!dst.hasRemaining()) return CoderResult.OVERFLOW; mark++; dst.put(c); } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { if (buffer == null) { assert decoder != null; return decoder.decode(in, out, false); } if (cont) { return flushHead (in,out); } if (buffer.remaining() == 0) { return handleHead (in,out); } else if (buffer.remaining() < in.remaining()) { int limit = in.limit(); in.limit(in.position()+buffer.remaining()); buffer.put(in); in.limit(limit); return handleHead (in, out); } else { buffer.put(in); return CoderResult.UNDERFLOW; } }
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"); //NOI18N } } // Discard the bytes that have been read decoderIn.compact(); }
@Override protected CoderResult implFlush(ByteBuffer out) { CoderResult res; if (buffer != null) { res = handleHead(null, out); return res; } else if (remainder != null) { encoder.encode(remainder, out, true); } else { CharBuffer empty = (CharBuffer) CharBuffer.allocate(0).flip(); encoder.encode(empty, out, true); } res = encoder.flush(out); return res; }
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { byte b = src.get(); if (!dst.hasRemaining()) return CoderResult.OVERFLOW; dst.put((char)(b & 0xff)); mark++; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
/** * 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(); }
String toString(byte[] ba, int length) { CharsetDecoder cd = decoder().reset(); int len = (int)(length * cd.maxCharsPerByte()); char[] ca = new char[len]; if (len == 0) return new String(ca); // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. ZipCoder uses // REPORT mode. if (isUTF8 && cd instanceof ArrayDecoder) { int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca); if (clen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return new String(ca, 0, clen); } ByteBuffer bb = ByteBuffer.wrap(ba, 0, length); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = cd.decode(bb, cb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = cd.flush(cb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); return new String(ca, 0, cb.position()); }
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); byte[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); int len = Math.min(dl - dp, sl - sp); while (len-- > 0) { char c = sa[sp]; int b = encode(c); if (b == UNMAPPABLE_ENCODING) { if (Character.isSurrogate(c)) { if (sgp == null) sgp = new Surrogate.Parser(); if (sgp.parse(c, sa, sp, sl) < 0) { return withResult(sgp.error(), src, sp, dst, dp); } return withResult(sgp.unmappableResult(), src, sp, dst, dp); } return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp); } da[dp++] = (byte)b; sp++; } return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW, src, sp, dst, dp); }
/** * Generates one or two UTF-16 characters to represent the given UCS-4 * character. * * @param uc The UCS-4 character * @param len The number of input bytes from which the UCS-4 value * was constructed (used when creating result objects) * @param dst The destination buffer, to which one or two UTF-16 * characters will be written * * @return Either a positive count of the number of UTF-16 characters * written to the destination buffer, or -1, in which case * error() will return a descriptive result object */ public int generate(int uc, int len, CharBuffer dst) { if (uc <= 0xffff) { if (is(uc)) { error = CoderResult.malformedForLength(len); return -1; } if (dst.remaining() < 1) { error = CoderResult.OVERFLOW; return -1; } dst.put((char)uc); error = null; return 1; } if (uc < UCS4_MIN) { error = CoderResult.malformedForLength(len); return -1; } if (uc <= UCS4_MAX) { if (dst.remaining() < 2) { error = CoderResult.OVERFLOW; return -1; } dst.put(high(uc)); dst.put(low(uc)); error = null; return 2; } error = CoderResult.unmappableForLength(len); return -1; }
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { if (src.hasArray() && dst.hasArray()) return encodeArrayLoop(src, dst); else return encodeBufferLoop(src, dst); }
private static CoderResult malformed(ByteBuffer src, int sp, CharBuffer dst, int dp, int nb) { src.position(sp - src.arrayOffset()); CoderResult cr = malformedN(src, nb); updatePositions(src, sp, dst, dp); return cr; }
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); try { while (sp < sl) { byte b = sa[sp]; if (dp >= dl) return CoderResult.OVERFLOW; da[dp++] = (char)(b & 0xff); sp++; } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { if (src.hasArray() && dst.hasArray()) return decodeArrayLoop(src, dst); else return decodeBufferLoop(src, dst); }
@Override protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { if (in.hasArray() && out.hasArray()) { return decodeHasArray(in, out); } return decodeNotHasArray(in, out); }
@Override protected CoderResult implFlush(CharBuffer out) { lastCharBuffer = null; if (buffer != null) { return decodeHead(null, out, true); } else { currentDecoder.decode(EMPTY_BYTE_BUFFER, out, true); return currentDecoder.flush(out); } }
private CoderResult flushHead (CharBuffer in , ByteBuffer out) { buffer.flip(); CoderResult r = encoder.encode(buffer,out, in==null); if (r.isOverflow()) { cont = true; return r; } buffer = null; if (in == null) { return r; } return encoder.encode(in, out, false); }
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { int mark = src.position(); try { while (src.hasRemaining()) { char[] cc = null; int b1 = src.get() & 0xff; char c = decodeSingle(b1); int inSize = 1, outSize = 1; if (c == UNMAPPABLE) { if (src.remaining() < 1) return CoderResult.UNDERFLOW; int b2 = src.get() & 0xff; inSize++; c = decodeDouble(b1, b2); if (c == UNMAPPABLE) { cc = decodeDoubleEx(b1, b2); if (cc == null) { if (decodeSingle(b2) == UNMAPPABLE) return CoderResult.unmappableForLength(2); else return CoderResult.unmappableForLength(1); } outSize++; } } if (dst.remaining() < outSize) return CoderResult.OVERFLOW; if (outSize == 2) { dst.put(cc[0]); dst.put(cc[1]); } else { dst.put(c); } mark += inSize; } return CoderResult.UNDERFLOW; } finally { src.position(mark); } }
private int handleDecodingResult( final CoderResult result, final ru.radiomayak.http.util.CharArrayBuffer charbuffer, final ByteBuffer bbuf) throws IOException { if (result.isError()) { result.throwException(); } this.cbuf.flip(); final int len = this.cbuf.remaining(); while (this.cbuf.hasRemaining()) { charbuffer.append(this.cbuf.get()); } this.cbuf.compact(); return len; }
private void handleEncodingResult(final CoderResult result) throws IOException { if (result.isError()) { result.throwException(); } this.bbuf.flip(); while (this.bbuf.hasRemaining()) { write(this.bbuf.get()); } this.bbuf.compact(); }
protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (buffer == null) { assert encoder != null; if (remainder!=null) { CoderResult result = encoder.encode(remainder,out,false); if (!remainder.hasRemaining()) { remainder = null; } } CoderResult result = encoder.encode(in, out, false); return result; } if (buffer.remaining() == 0 || (buffer.position() > 0 && in.limit() == 0)) { return handleHead (in,out); } else if (buffer.remaining() < in.remaining()) { int limit = in.limit(); in.limit(in.position()+buffer.remaining()); buffer.put(in); in.limit(limit); return handleHead (in, out); } else { buffer.put(in); return CoderResult.UNDERFLOW; } }
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { if (buffer == null) { assert decoder != null; if (remainder!=null) { ByteBuffer tmp = ByteBuffer.allocate(remainder.remaining() + in.remaining()); tmp.put(remainder); tmp.put(in); tmp.flip(); CoderResult result = decoder.decode(tmp,out,false); if (tmp.hasRemaining()) { remainder = tmp; } else { remainder = null; } return result; } else { return decoder.decode(in, out, false); } } if (buffer.remaining() == 0) { return handleHead (in,out); } else if (buffer.remaining() < in.remaining()) { int limit = in.limit(); in.limit(in.position()+buffer.remaining()); buffer.put(in); in.limit(limit); return handleHead (in, out); } else { buffer.put(in); return CoderResult.UNDERFLOW; } }
private static CoderResult malformedForLength(ByteBuffer src, int mark, int malformedNB) { src.position(mark); return CoderResult.malformedForLength(malformedNB); }
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int soff = src.arrayOffset(); int sp = soff + src.position(); int sl = soff + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] da = dst.array(); int doff = dst.arrayOffset(); int dp = doff + dst.position(); int dl = doff + dst.limit(); assert (dp <= dl); dp = (dp <= dl ? dp : dl); int dlen = dl - dp; int slen = sl - sp; int len = (dlen < slen) ? dlen : slen; try { int ret = encodeISOArray(sa, sp, da, dp, len); sp = sp + ret; dp = dp + ret; if (ret != len) { if (sgp.parse(sa[sp], sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } if (len < slen) return CoderResult.OVERFLOW; return CoderResult.UNDERFLOW; } finally { src.position(sp - soff); dst.position(dp - doff); } }
/** * Generates one or two UTF-16 characters to represent the given UCS-4 * character. * * @param uc The UCS-4 character * @param len The number of input bytes from which the UCS-4 value * was constructed (used when creating result objects) * @param da The destination array, to which one or two UTF-16 * characters will be written * @param dp The destination position * @param dl The destination limit * * @returns Either a positive count of the number of UTF-16 characters * written to the destination buffer, or -1, in which case * error() will return a descriptive result object */ public int generate(int uc, int len, char[] da, int dp, int dl) { if (Character.isBmpCodePoint(uc)) { char c = (char) uc; if (Character.isSurrogate(c)) { error = CoderResult.malformedForLength(len); return -1; } if (dl - dp < 1) { error = CoderResult.OVERFLOW; return -1; } da[dp] = c; error = null; return 1; } else if (Character.isValidCodePoint(uc)) { if (dl - dp < 2) { error = CoderResult.OVERFLOW; return -1; } da[dp] = Character.highSurrogate(uc); da[dp + 1] = Character.lowSurrogate(uc); error = null; return 2; } else { error = CoderResult.unmappableForLength(len); return -1; } }
private static CoderResult lookupN(ByteBuffer src, int n) { for (int i = 1; i < n; i++) { if (isNotContinuation(src.get())) return CoderResult.malformedForLength(i); } return CoderResult.malformedForLength(n); }
byte[] encode(char[] ca, int off, int len) { int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }
static void test(String expectedCharset, byte[] input) throws Exception { Charset cs = Charset.forName("x-JISAutoDetect"); CharsetDecoder autoDetect = cs.newDecoder(); Charset cs2 = Charset.forName(expectedCharset); CharsetDecoder decoder = cs2.newDecoder(); ByteBuffer bb = ByteBuffer.allocate(128); CharBuffer charOutput = CharBuffer.allocate(128); CharBuffer charExpected = CharBuffer.allocate(128); bb.put(input); bb.flip(); bb.mark(); CoderResult result = autoDetect.decode(bb, charOutput, true); checkCoderResult(result); charOutput.flip(); String actual = charOutput.toString(); bb.reset(); result = decoder.decode(bb, charExpected, true); checkCoderResult(result); charExpected.flip(); String expected = charExpected.toString(); check(actual.equals(expected), String.format("actual=%s expected=%s", actual, expected)); }