下面列出了怎么用java.nio.charset.CoderResult的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
@Override
protected CoderResult implFlush(CharBuffer out) {
CoderResult res;
if (buffer != null) {
res = handleHead(null, out);
return res;
}
else if (remainder != null) {
decoder.decode(remainder, out, true);
}
else {
ByteBuffer empty = (ByteBuffer) ByteBuffer.allocate(0).flip();
decoder.decode(empty, out, true);
}
res = decoder.flush(out);
return res;
}
/**
* <p>Decodes a byte in <i>base 64 mode</i>. Will directly write a character to the output
* buffer if completed.</p>
*
* @param in The input buffer
* @param out The output buffer
* @param lastRead Last byte read from the input buffer
* @return CoderResult.malformed if a non-base 64 character was encountered in strict
* mode, null otherwise
*/
private CoderResult handleBase64(ByteBuffer in, CharBuffer out, byte lastRead) {
CoderResult result = null;
int sextet = base64.getSextet(lastRead);
if (sextet >= 0) {
bitsRead += 6;
if (bitsRead < 16) {
tempChar += sextet << (16 - bitsRead);
} else {
bitsRead -= 16;
tempChar += sextet >> (bitsRead);
out.put((char) tempChar);
tempChar = (sextet << (16 - bitsRead)) & 0xFFFF;
}
} else {
if (strict)
return malformed(in);
out.put((char) lastRead);
if (base64bitsWaiting())
result = malformed(in);
setUnshifted();
}
return result;
}
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());
}
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
private static 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:
assert false;
return null;
}
}
/**
* Write length delimited string
*
* @param s value to be written with integer
* @param index column index to put in warning
* @exception DRDAProtocolException
*/
protected void writeLDString(String s, int index) throws DRDAProtocolException
{
// Position on which to write the length of the string (in bytes). The
// actual writing of the length is delayed until we have encoded the
// string.
final int lengthPos = buffer.position();
// Position on which to start writing the string (right after length,
// which is 2 bytes long).
final int stringPos = lengthPos + 2;
// don't send more than LONGVARCHAR_MAX_LEN bytes
final int maxStrLen =
Math.min(maxEncodedLength(s), FdocaConstants.LONGVARCHAR_MAX_LEN);
ensureLength(2 + maxStrLen);
// limit the writable area of the output buffer
buffer.position(stringPos);
buffer.limit(stringPos + maxStrLen);
// encode the string
CharBuffer input = CharBuffer.wrap(s);
CoderResult res = encoder.encode(input, buffer, true);
if (SanityManager.DEBUG) {
// UNDERFLOW is returned if the entire string was encoded, OVERFLOW
// is returned if the string was truncated at LONGVARCHAR_MAX_LEN
// GemStone changes BEGIN
if (res != CoderResult.UNDERFLOW && res != CoderResult.OVERFLOW)
// GemStone changes END
SanityManager.ASSERT(
res == CoderResult.UNDERFLOW || res == CoderResult.OVERFLOW,
"Unexpected coder result: " + res);
}
// write the length in bytes
buffer.putShort(lengthPos, (short) (maxStrLen - buffer.remaining()));
// remove the limit on the output buffer
buffer.limit(buffer.capacity());
}
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) {
int b = sa[sp];
char c = decode(b);
if (c == '\uFFFD')
return CoderResult.unmappableForLength(1);
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = c;
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult implFlush(ByteBuffer out) {
if (currentState == DBCS) {
if (out.remaining() < 1)
return CoderResult.OVERFLOW;
out.put(SI);
}
implReset();
return CoderResult.UNDERFLOW;
}
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 decodeMoreBytesToCharacterBuffer(boolean endOfInput) throws DecodingException {
final CoderResult coderResult = decoder.decode(buffer, charBuffer, endOfInput);
if (coderResult.isOverflow()) {
upsizeCharBuffer();
return decodeMoreBytesToCharacterBuffer(endOfInput);
} else if (coderResult.isError()) {
throw new DecodingException(HumanReadableText.BAD_IO_ENCODING, "Bad character encoding");
} else if (coderResult.isUnderflow()) {
buffer.clear();
}
return coderResult;
}
private static 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:
assert false;
return null;
}
}
/**
* Implementation of the encoding loop. Apply Perforce specific updates,
* then reset the encoder and encode the characters to bytes.
*/
protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
CharBuffer tmpcb = CharBuffer.allocate(cb.remaining());
while (cb.hasRemaining()) {
tmpcb.put(cb.get());
}
tmpcb.rewind();
update(tmpcb);
encoder.reset();
CoderResult cr = encoder.encode(tmpcb, bb, true);
cb.position(cb.position() - tmpcb.remaining());
return (cr);
}
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();
CoderResult cr = CoderResult.UNDERFLOW;
if ((dl - dp) < (sl - sp)) {
sl = sp + (dl - dp);
cr = CoderResult.OVERFLOW;
}
while (sp < sl) {
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(cr, src, sp, dst, dp);
}
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
protected CoderResult decodeLoop(ByteBuffer src,
CharBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return decodeArrayLoop(src, dst);
else
return decodeBufferLoop(src, dst);
}
char[] decode(byte[] ba, int off, int len) {
int en = scale(len, cd.maxCharsPerByte());
char[] ca = new char[en];
if (len == 0)
return ca;
if (cd instanceof ArrayDecoder) {
int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
return safeTrim(ca, clen, cs, isTrusted);
} else {
cd.reset();
ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
CharBuffer cb = CharBuffer.wrap(ca);
try {
CoderResult cr = cd.decode(bb, cb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = cd.flush(cb);
if (!cr.isUnderflow())
cr.throwException();
} catch (CharacterCodingException x) {
// Substitution is always enabled,
// so this shouldn't happen
throw new Error(x);
}
return safeTrim(ca, cb.position(), cs, isTrusted);
}
}
private static final CoderResult withResult(CoderResult cr,
Buffer src, int sp,
Buffer dst, int dp)
{
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
return cr;
}
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 static void decodeString(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);
}
}
protected CoderResult decodeLoop(ByteBuffer src,
CharBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return decodeArrayLoop(src, dst);
else
return decodeBufferLoop(src, dst);
}
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
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());
}
}
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
public void testMalformedSurrogatePair() throws Exception {
// malformed: low surrogate first is detected as an error.
Charset cs = Charset.forName("UTF-32BE");
CharsetEncoder e = cs.newEncoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '\udf9f' }), bb, false);
assertTrue(cr.toString(), cr.isMalformed());
assertEquals(1, cr.length());
}
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;
}
/**
* Test method isError().
*
*/
public void testIsError() {
assertFalse(CoderResult.UNDERFLOW.isError());
assertFalse(CoderResult.OVERFLOW.isError());
assertTrue(CoderResult.malformedForLength(1).isError());
assertTrue(CoderResult.unmappableForLength(1).isError());
}
char[] decode(byte[] ba, int off, int len) {
int en = scale(len, cd.maxCharsPerByte());
char[] ca = new char[en];
if (len == 0)
return ca;
if (cd instanceof ArrayDecoder) {
int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
return safeTrim(ca, clen, cs, isTrusted);
} else {
cd.reset();
ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
CharBuffer cb = CharBuffer.wrap(ca);
try {
CoderResult cr = cd.decode(bb, cb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = cd.flush(cb);
if (!cr.isUnderflow())
cr.throwException();
} catch (CharacterCodingException x) {
// Substitution is always enabled,
// so this shouldn't happen
throw new Error(x);
}
return safeTrim(ca, cb.position(), cs, isTrusted);
}
}