本文整理匯總了Java中java.nio.charset.CoderResult類的典型用法代碼示例。如果您正苦於以下問題:Java CoderResult類的具體用法?Java CoderResult怎麽用?Java CoderResult使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CoderResult類屬於java.nio.charset包,在下文中一共展示了CoderResult類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: flush
import java.nio.charset.CoderResult; //導入依賴的package包/類
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;
}
}
示例2: encodeBufferLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
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);
}
}
示例3: byCharsetEncoder_US_ASCII
import java.nio.charset.CoderResult; //導入依賴的package包/類
@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);
}
}
示例4: decodeBufferLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
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);
}
}
示例5: decodeLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
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;
}
}
示例6: processInput
import java.nio.charset.CoderResult; //導入依賴的package包/類
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();
}
示例7: implFlush
import java.nio.charset.CoderResult; //導入依賴的package包/類
@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;
}
示例8: decodeBufferLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
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);
}
}
示例9: processInput
import java.nio.charset.CoderResult; //導入依賴的package包/類
/**
* 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();
}
示例10: toString
import java.nio.charset.CoderResult; //導入依賴的package包/類
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());
}
示例11: encodeArrayLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
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);
}
示例12: generate
import java.nio.charset.CoderResult; //導入依賴的package包/類
/**
* 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;
}
示例13: encodeLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
protected CoderResult encodeLoop(CharBuffer src,
ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
示例14: encodeLoop
import java.nio.charset.CoderResult; //導入依賴的package包/類
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst)
{
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
示例15: malformed
import java.nio.charset.CoderResult; //導入依賴的package包/類
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;
}