private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { char[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); assert (sp <= sl); sp = (sp <= sl ? sp : sl); byte[] 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) { char c = sa[sp]; if (c < 0x80) { if (dp >= dl) return CoderResult.OVERFLOW; da[dp] = (byte) c; sp++; dp++; continue; } if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error(); return sgp.unmappableResult(); } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
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 (b >= 0) { if (dp >= dl) return CoderResult.OVERFLOW; da[dp++] = (char) b; sp++; continue; } return CoderResult.malformedForLength(1); } return CoderResult.UNDERFLOW; } finally { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); } }
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); CoderResult cr = CoderResult.UNDERFLOW; if ((dl - dp) < (sl - sp)) { sl = sp + (dl - dp); cr = CoderResult.OVERFLOW; } while (sp < sl) { char c = decode(sa[sp]); if (c == UNMAPPABLE_DECODING) { return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp); } da[dp++] = c; sp++; } return withResult(cr, src, sp, dst, dp); }
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); }
private BigDecimal toBigDecimal() { if (lineBuffer.hasArray()) { char[] array = lineBuffer.array(); int offset = lineBuffer.arrayOffset() + lineBuffer.position(); int length = lineBuffer.remaining(); return new BigDecimal(array, offset, length); } else { return new BigDecimal(lineBuffer.toString()); } }
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 dlASCII = dp + Math.min(sl - sp, dl - dp); // ASCII only loop while (dp < dlASCII && sa[sp] < '\u0080') da[dp++] = (byte) sa[sp++]; while (sp < sl) { char c = sa[sp]; if (c < 0x80) { // Have at most seven bits if (dp >= dl) return overflow(src, sp, dst, dp); da[dp++] = (byte) c; } else if (c < 0x800) { // 2 bytes, 11 bits if (dl - dp < 2) return overflow(src, sp, dst, dp); da[dp++] = (byte) (0xc0 | (c >> 6)); da[dp++] = (byte) (0x80 | (c & 0x3f)); } else if (isSurrogate(c)) { // Have a surrogate pair if (sgp == null) sgp = new Parser(); int uc = sgp.parse(c, sa, sp, sl); if (uc < 0) { updatePositions(src, sp, dst, dp); return sgp.error(); } if (dl - dp < 4) return overflow(src, sp, dst, dp); da[dp++] = (byte) (0xf0 | ((uc >> 18))); da[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f)); da[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f)); da[dp++] = (byte) (0x80 | (uc & 0x3f)); sp++; // 2 chars } else { // 3 bytes, 16 bits if (dl - dp < 3) return overflow(src, sp, dst, dp); da[dp++] = (byte) (0xe0 | ((c >> 12))); da[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f)); da[dp++] = (byte) (0x80 | (c & 0x3f)); } sp++; } updatePositions(src, sp, dst, dp); return CoderResult.UNDERFLOW; }
/** * Creates a new big-endian buffer whose content is a subregion of the specified {@code string} * encoded in the specified {@code charset}. The new buffer's {@code readerIndex} and {@code * writerIndex} are {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (length == 0) { return EMPTY_BUFFER; } if (string instanceof CharBuffer) { CharBuffer buf = (CharBuffer) string; if (buf.hasArray()) { return copiedBuffer( buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset); } buf = buf.slice(); buf.limit(length); buf.position(offset); return copiedBuffer(buf, charset); } return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset); }
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { // This method is optimized for ASCII input. byte[] sa = src.array(); int sp = src.arrayOffset() + src.position(); int sl = src.arrayOffset() + src.limit(); char[] da = dst.array(); int dp = dst.arrayOffset() + dst.position(); int dl = dst.arrayOffset() + dst.limit(); int dlASCII = dp + Math.min(sl - sp, dl - dp); // ASCII only loop while (dp < dlASCII && sa[sp] >= 0) da[dp++] = (char) sa[sp++]; while (sp < sl) { int b1 = sa[sp]; if (b1 >= 0) { // 1 byte, 7 bits: 0xxxxxxx if (dp >= dl) return xflow(src, sp, sl, dst, dp, 1); da[dp++] = (char) b1; sp++; } else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) { // 2 bytes, 11 bits: 110xxxxx 10xxxxxx if (sl - sp < 2 || dp >= dl) return xflow(src, sp, sl, dst, dp, 2); int b2 = sa[sp + 1]; if (isNotContinuation(b2)) return malformedForLength(src, sp, dst, dp, 1); da[dp++] = (char) (((b1 << 6) ^ b2) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80))); sp += 2; } else if ((b1 >> 4) == -2) { // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx int srcRemaining = sl - sp; if (srcRemaining < 3 || dp >= dl) { if (srcRemaining > 1 && isMalformed3_2(b1, sa[sp + 1])) return malformedForLength(src, sp, dst, dp, 1); return xflow(src, sp, sl, dst, dp, 3); } int b2 = sa[sp + 1]; int b3 = sa[sp + 2]; if (isMalformed3(b1, b2, b3)) return malformed(src, sp, dst, dp, 3); char c = (char) ((b1 << 12) ^ (b2 << 6) ^ (b3 ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); if (isSurrogate(c)) return malformedForLength(src, sp, dst, dp, 3); da[dp++] = c; sp += 3; } else if ((b1 >> 3) == -2) { // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx int srcRemaining = sl - sp; if (srcRemaining < 4 || dl - dp < 2) { if (srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1])) return malformedForLength(src, sp, dst, dp, 1); if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2])) return malformedForLength(src, sp, dst, dp, 2); return xflow(src, sp, sl, dst, dp, 4); } int b2 = sa[sp + 1]; int b3 = sa[sp + 2]; int b4 = sa[sp + 3]; int uc = ((b1 << 18) ^ (b2 << 12) ^ (b3 << 6) ^ (b4 ^ (((byte) 0xF0 << 18) ^ ((byte) 0x80 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80)))); if (isMalformed4(b2, b3, b4) || // shortest form check !Character.isSupplementaryCodePoint(uc)) { return malformed(src, sp, dst, dp, 4); } da[dp++] = highSurrogate(uc); da[dp++] = lowSurrogate(uc); sp += 4; } else return malformed(src, sp, dst, dp, 1); } return xflow(src, sp, sl, dst, dp, 0); }
private static void updatePositions(CharBuffer src, int sp, ByteBuffer dst, int dp) { src.position(sp - src.arrayOffset()); dst.position(dp - dst.arrayOffset()); }
/** * Construct from array backed CharBuffer. The buffer position will not be altered as part of read * operations. * * @throws UnsupportedOperationException if buffer isn't backed by an array. * @throws ReadOnlyBufferException if buffer is is read-only. */ public CharArrayReader(CharBuffer buffer) { this(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); }
protected CoderResult decodeLoop(ByteBuffer bb, CharBuffer cb) { int cbRemaining = cb.remaining(); if (CharsetProviderImpl.hasLoadedNatives() && bb.isDirect() && bb.hasRemaining() && cb.hasArray()) { int toProceed = bb.remaining(); int cbPos = cb.position(); int bbPos = bb.position(); boolean throwOverflow = false; if (cbRemaining < toProceed) { toProceed = cbRemaining; throwOverflow = true; } int res = nDecode( cb.array(), cb.arrayOffset() + cbPos, toProceed, AddressUtil.getDirectBufferAddress(bb), bbPos); bb.position(bbPos + res); cb.position(cbPos + res); if (throwOverflow) return CoderResult.OVERFLOW; } else { if (bb.hasArray() && cb.hasArray()) { int rem = bb.remaining(); rem = cbRemaining >= rem ? rem : cbRemaining; byte[] bArr = bb.array(); char[] cArr = cb.array(); int bStart = bb.position(); int cStart = cb.position(); int i; for (i = bStart; i < bStart + rem; i++) { char in = (char) (bArr[i] & 0xFF); if (in >= 4) { int index = (int) in - 4; cArr[cStart++] = (char) arr[index]; } else { cArr[cStart++] = (char) (in & 0xFF); } } bb.position(i); cb.position(cStart); if (rem == cbRemaining && bb.hasRemaining()) return CoderResult.OVERFLOW; } else { while (bb.hasRemaining()) { if (cbRemaining == 0) return CoderResult.OVERFLOW; char in = (char) (bb.get() & 0xFF); if (in >= 4) { int index = (int) in - 4; cb.put(arr[index]); } else { cb.put((char) (in & 0xFF)); } cbRemaining--; } } } return CoderResult.UNDERFLOW; }
protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) { int bbRemaining = bb.remaining(); if (CharsetProviderImpl.hasLoadedNatives() && bb.isDirect() && cb.hasRemaining() && cb.hasArray()) { int toProceed = cb.remaining(); int cbPos = cb.position(); int bbPos = bb.position(); boolean throwOverflow = false; if (bbRemaining < toProceed) { toProceed = bbRemaining; throwOverflow = true; } int[] res = {toProceed, 0}; nEncode( AddressUtil.getDirectBufferAddress(bb), bbPos, cb.array(), cb.arrayOffset() + cbPos, res); if (res[0] <= 0) { bb.position(bbPos - res[0]); cb.position(cbPos - res[0]); if (res[1] != 0) { if (res[1] < 0) return CoderResult.malformedForLength(-res[1]); else return CoderResult.unmappableForLength(res[1]); } } else { bb.position(bbPos + res[0]); cb.position(cbPos + res[0]); if (throwOverflow) return CoderResult.OVERFLOW; } } else { if (bb.hasArray() && cb.hasArray()) { byte[] byteArr = bb.array(); char[] charArr = cb.array(); int rem = cb.remaining(); int byteArrStart = bb.position(); rem = bbRemaining <= rem ? bbRemaining : rem; int x; for (x = cb.position(); x < cb.position() + rem; x++) { char c = charArr[x]; if (c > (char) 0x20AC) { if (c >= 0xD800 && c <= 0xDFFF) { if (x + 1 < cb.limit()) { char c1 = charArr[x + 1]; if (c1 >= 0xD800 && c1 <= 0xDFFF) { cb.position(x); bb.position(byteArrStart); return CoderResult.unmappableForLength(2); } } else { cb.position(x); bb.position(byteArrStart); return CoderResult.UNDERFLOW; } cb.position(x); bb.position(byteArrStart); return CoderResult.malformedForLength(1); } cb.position(x); bb.position(byteArrStart); return CoderResult.unmappableForLength(1); } else { if (c < 0x04) { byteArr[byteArrStart++] = (byte) c; } else { int index = (int) c >> 8; index = encodeIndex[index]; if (index < 0) { cb.position(x); bb.position(byteArrStart); return CoderResult.unmappableForLength(1); } index <<= 8; index += (int) c & 0xFF; if ((byte) arr[index] != 0) { byteArr[byteArrStart++] = (byte) arr[index]; } else { cb.position(x); bb.position(byteArrStart); return CoderResult.unmappableForLength(1); } } } } cb.position(x); bb.position(byteArrStart); if (rem == bbRemaining && cb.hasRemaining()) { return CoderResult.OVERFLOW; } } else { while (cb.hasRemaining()) { if (bbRemaining == 0) return CoderResult.OVERFLOW; char c = cb.get(); if (c > (char) 0x20AC) { if (c >= 0xD800 && c <= 0xDFFF) { if (cb.hasRemaining()) { char c1 = cb.get(); if (c1 >= 0xD800 && c1 <= 0xDFFF) { cb.position(cb.position() - 2); return CoderResult.unmappableForLength(2); } else { cb.position(cb.position() - 1); } } else { cb.position(cb.position() - 1); return CoderResult.UNDERFLOW; } cb.position(cb.position() - 1); return CoderResult.malformedForLength(1); } cb.position(cb.position() - 1); return CoderResult.unmappableForLength(1); } else { if (c < 0x04) { bb.put((byte) c); } else { int index = (int) c >> 8; index = encodeIndex[index]; if (index < 0) { cb.position(cb.position() - 1); return CoderResult.unmappableForLength(1); } index <<= 8; index += (int) c & 0xFF; if ((byte) arr[index] != 0) { bb.put((byte) arr[index]); } else { cb.position(cb.position() - 1); return CoderResult.unmappableForLength(1); } } bbRemaining--; } } } } return CoderResult.UNDERFLOW; }