/* * Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// only used when there is only one byte left in src buffer privatestaticboolean isMalformed3_2(int b1, int b2) { return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
(b2 & 0xc0) != 0x80;
}
privatestatic CoderResult malformedN(ByteBuffer src, int nb) { switch (nb) { case 1: case 2: // always 1 return CoderResult.malformedForLength(1); case 3: int b1 = src.get(); int b2 = src.get(); // no need to lookup b3 return CoderResult.malformedForLength(
((b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
isNotContinuation(b2)) ? 1 : 2); case 4: // we don't care the speed here
b1 = src.get() & 0xff;
b2 = src.get() & 0xff; if (b1 > 0xf4 ||
(b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) ||
(b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
isNotContinuation(b2)) return CoderResult.malformedForLength(1); if (isNotContinuation(src.get())) return CoderResult.malformedForLength(2); return CoderResult.malformedForLength(3); default: assertfalse; returnnull;
}
}
privatestatic 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;
}
privatestatic CoderResult malformed(ByteBuffer src, int mark, int nb)
{
src.position(mark);
CoderResult cr = malformedN(src, nb);
src.position(mark); return cr;
}
privatestatic CoderResult malformedForLength(ByteBuffer src, int sp,
CharBuffer dst, int dp, int malformedNB)
{
updatePositions(src, sp, dst, dp); return CoderResult.malformedForLength(malformedNB);
}
privatestatic CoderResult malformedForLength(ByteBuffer src, int mark, int malformedNB)
{
src.position(mark); return CoderResult.malformedForLength(malformedNB);
}
privatestatic CoderResult xflow(Buffer src, int sp, int sl,
Buffer dst, int dp, int nb) {
updatePositions(src, sp, dst, dp); return (nb == 0 || sl - sp < nb)
? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
}
private CoderResult decodeArrayLoop(ByteBuffer src,
CharBuffer dst)
{ // This method is optimized for ASCII input. byte[] sa = src.array(); int soff = src.arrayOffset(); int sp = soff + src.position(); int sl = soff + src.limit();
char[] da = dst.array(); int doff = dst.arrayOffset(); int dp = doff + dst.position(); int dl = doff + dst.limit();
int n = JLA.decodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
sp += n;
dp += n;
privatestatic ByteBuffer getByteBuffer(ByteBuffer bb, byte[] ba, int sp)
{ if (bb == null)
bb = ByteBuffer.wrap(ba);
bb.position(sp); return bb;
}
// returns -1 if there is/are malformed byte(s) and the // "action" for malformed input is not REPLACE. publicint decode(byte[] sa, int sp, int len, char[] da) { finalint sl = sp + len; int dp = 0; int dlASCII = Math.min(len, da.length);
ByteBuffer bb = null; // only necessary if malformed
// ASCII only optimized loop while (dp < dlASCII && sa[sp] >= 0)
da[dp++] = (char) sa[sp++];
// returns -1 if there is malformed char(s) and the // "action" for malformed input is not REPLACE. publicint encode(char[] sa, int sp, int len, byte[] da) { int sl = sp + len; int dp = 0;
// Handle ASCII-only prefix int n = JLA.encodeASCII(sa, sp, da, dp, Math.min(len, da.length));
sp += n;
dp += n;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.