下面列出了java.nio.charset.CoderResult# isOverflow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
public void flush() throws IOException {
if (pos > 0) {
if (out != null) {
CharBuffer charBuffer = CharBuffer.wrap(text, 0, pos);
CoderResult result = charset.encode(charBuffer, bytes, true);
while (true) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
flushBytes();
result = charset.encode(charBuffer, bytes, true);
continue;
}
break;
}
flushBytes();
out.flush();
} else {
writer.write(text, 0, pos);
writer.flush();
}
pos = 0;
}
}
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;
}
}
/**
* If the CoderResult indicates the ByteBuffer is full, synchronize on the destination and write the content
* of the ByteBuffer to the destination. If the specified ByteBuffer is owned by the destination, we have
* reached the end of a MappedBuffer and we call drain() on the destination to remap().
* <p>
* If the CoderResult indicates more can be encoded, this method does nothing and returns the temp ByteBuffer.
* </p>
*
* @param destination the destination to write bytes to
* @param temp the ByteBuffer containing the encoded bytes. May be a temporary buffer or may be the ByteBuffer of
* the ByteBufferDestination
* @param result the CoderResult from the CharsetEncoder
* @return the ByteBuffer to encode into for the remainder of the text
*/
private static ByteBuffer drainIfByteBufferFull(final ByteBufferDestination destination, final ByteBuffer temp,
final CoderResult result) {
if (result.isOverflow()) { // byte buffer full
// all callers already synchronize on destination but for safety ensure we are synchronized because
// below calls to drain() may cause destination to swap in a new ByteBuffer object
synchronized (destination) {
final ByteBuffer destinationBuffer = destination.getByteBuffer();
if (destinationBuffer != temp) {
temp.flip();
ByteBufferDestinationHelper.writeToUnsynchronized(temp, destination);
temp.clear();
return destination.getByteBuffer();
} else {
return destination.drain(destinationBuffer);
}
}
} else {
return temp;
}
}
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;
}
}
/**
* Decode the buffer using the CharsetDecoder.
* @param byteBuf
* @return false if failed because the charbuffer was not big enough
* @throws RuntimeException if it fails for encoding errors
*/
private boolean decodeUT8(ByteBuffer byteBuf) {
// We give it all of the input data in call.
boolean endOfInput = true;
decoder.reset();
charBuffer.rewind();
// Convert utf-8 bytes to sequence of chars
CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput);
if (result.isOverflow()) {
// Not enough space in the charBuffer.
return false;
} else if (result.isError()) {
// Any other error
try {
result.throwException();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
return true;
}
Validity checkValidity(ByteBuffer buffer) {
int pos = buffer.position();
CoderResult result = CoderResult.OVERFLOW;
while (result.isOverflow()) {
dummyBuffer.clear();
result = decoder.decode(buffer, dummyBuffer, true);
}
buffer.position(pos);
if (result.isUnderflow()) {
return Validity.VALID;
} else if (result.isMalformed()) {
return Validity.MALFORMED;
} else {
return Validity.UNMAPPABLE;
}
}
private CoderResult flushHead (CharBuffer in , ByteBuffer out) {
buffer.flip();
CoderResult r = encoder.encode(buffer,out, in==null);
if (r.isOverflow()) {
remainder = buffer;
buffer = null;
return r;
}
else {
buffer = null;
if (in == null) {
return r;
}
return encoder.encode(in, out, false);
}
}
/**
* 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();
}
/**
* 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(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");
}
}
// Discard the bytes that have been read
decoderIn.compact();
}
/**
* Decode the buffer using the CharsetDecoder.
* @param byteBuf
* @return false if failed because the charbuffer was not big enough
* @throws RuntimeException if it fails for encoding errors
*/
private boolean decodeUT8(ByteBuffer byteBuf) {
// We give it all of the input data in call.
boolean endOfInput = true;
decoder.reset();
charBuffer.rewind();
// Convert utf-8 bytes to sequence of chars
CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput);
if (result.isOverflow()) {
// Not enough space in the charBuffer.
return false;
} else if (result.isError()) {
// Any other error
try {
result.throwException();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
return true;
}
private void byte2char(ByteBuffer bytes, CharBuffer chars) throws IOException {
decoder.reset();
chars.clear();
CoderResult result = decoder.decode(bytes, chars, true);
if (result.isError() || result.isOverflow()) {
throw new IOException(result.toString());
} else if (result.isUnderflow()) {
chars.flip();
}
}
private void flush() throws DecodingException {
final CoderResult coderResult = decoder.flush(charBuffer);
if (coderResult.isOverflow()) {
upsizeCharBuffer();
flush();
} else if (coderResult.isError()) {
throw new DecodingException(HumanReadableText.BAD_IO_ENCODING, "Bad character encoding");
}
}
private void convert(CharBuffer chars) throws IOException {
while (true) {
CoderResult result = encoder.encode(chars, bytes, false);
if (result.isOverflow()) {
// Make room and try again.
flushBytes(false);
continue;
} else if (result.isError()) {
result.throwException();
}
break;
}
}
/**
* Left in for reference. Less efficient, but potentially catches more errors. Behavior is
* largely dependent on how strict the JVM's utf8 decoder is. It is possible on some JVMs to
* decode a string that then throws an error when attempting to return it to bytes.
*
* @param bytes Bytes representing a utf8 string
* @return The decoded string
*/
private String decodeStringStreaming(byte[] bytes) {
try {
ByteBuffer input = getBuffer(bytes);
int bufSize = (int) (input.remaining() * localDecoder.get().averageCharsPerByte());
CharBuffer output = CharBuffer.allocate(bufSize);
for (; ; ) {
CoderResult result = localDecoder.get().decode(input, output, false);
if (result.isError()) {
return null;
}
if (result.isUnderflow()) {
break;
}
if (result.isOverflow()) {
// We need more room in our output buffer
bufSize = 2 * bufSize + 1;
CharBuffer o = CharBuffer.allocate(bufSize);
output.flip();
o.put(output);
output = o;
}
}
if (input.remaining() > 0) {
carryOver = input;
}
// Re-encode to work around bugs in UTF-8 decoder
CharBuffer test = CharBuffer.wrap(output);
localEncoder.get().encode(test);
output.flip();
String text = output.toString();
return text;
} catch (CharacterCodingException e) {
return null;
}
}
static byte[] encodeString(String str, int length) {
byte[] array = new byte[length];
Arrays.fill(array, (byte)0x20);
ByteBuffer buffer = ByteBuffer.wrap(array);
CharBuffer charBuffer = CharBuffer.wrap(str);
CoderResult result = ENCODER.get().encode(charBuffer, buffer, true);
if (result.isOverflow()) {
throw new EPSCommonException("The length of value = [" + str
+ "] is greater than length specified in the dictionary.");
}
return array;
}
public void close() {
if (outputStream.getServletRequestContext().getOriginalRequest().getDispatcherType() == DispatcherType.INCLUDE) {
return;
}
if (closed) {
return;
}
closed = true;
try {
boolean done = false;
CharBuffer buffer;
if (underflow == null) {
buffer = CharBuffer.wrap(EMPTY_CHAR);
} else {
buffer = CharBuffer.wrap(underflow);
underflow = null;
}
if (charsetEncoder != null) {
do {
ByteBuffer out = outputStream.underlyingBuffer();
if (out == null) {
//servlet output stream has already been closed
error = true;
return;
}
CoderResult result = charsetEncoder.encode(buffer, out, true);
if (result.isOverflow()) {
outputStream.flushInternal();
if (out.remaining() == 0) {
outputStream.close();
error = true;
return;
}
} else {
done = true;
}
} while (!done);
}
outputStream.close();
} catch (IOException e) {
error = true;
}
}
@Override
public void write(Bytes out, @NotNull CharSequence cs) {
// Write the actual cs length for accurate StringBuilder.ensureCapacity() while reading
out.writeStopBit(cs.length());
long encodedSizePos = out.writePosition();
out.writeSkip(4);
charsetEncoder.reset();
inputBuffer.clear();
outputBuffer.clear();
int csPos = 0;
boolean endOfInput = false;
// this loop inspired by the CharsetEncoder.encode(CharBuffer) implementation
while (true) {
if (!endOfInput) {
int nextCsPos = Math.min(csPos + inputBuffer.remaining(), cs.length());
append(inputBuffer, cs, csPos, nextCsPos);
inputBuffer.flip();
endOfInput = nextCsPos == cs.length();
csPos = nextCsPos;
}
CoderResult cr = inputBuffer.hasRemaining() ?
charsetEncoder.encode(inputBuffer, outputBuffer, endOfInput) :
CoderResult.UNDERFLOW;
if (cr.isUnderflow() && endOfInput)
cr = charsetEncoder.flush(outputBuffer);
if (cr.isUnderflow()) {
if (endOfInput) {
break;
} else {
inputBuffer.compact();
continue;
}
}
if (cr.isOverflow()) {
outputBuffer.flip();
writeOutputBuffer(out);
outputBuffer.clear();
continue;
}
try {
cr.throwException();
} catch (CharacterCodingException e) {
throw new IORuntimeException(e);
}
}
outputBuffer.flip();
writeOutputBuffer(out);
out.writeInt(encodedSizePos, (int) (out.writePosition() - encodedSizePos - 4));
}
public void write(byte[] data, int start, int len) {
// Fill the byte buffer
int remaining = bBuf.remaining();
if (len > remaining) {
// Allocate a new buffer
ByteBuffer tmp = bBuf;
int length = tmp.position() + len;
bBuf = ByteBuffer.allocate(length);
tmp.flip();
bBuf.put(tmp);
}
bBuf.put(data, start, len);
bBuf.flip();
// Drain the byte buffer
while (true) {
IntBuffer iBuf = IntBuffer.allocate(bBuf.remaining());
CoderResult result = decoder.decode(bBuf, cBuf, false);
cBuf.flip();
while (cBuf.hasRemaining()) {
char c = cBuf.get();
if (Character.isSurrogate(c)) {
if (Character.isHighSurrogate(c)) {
if (cBuf.hasRemaining()) {
char low = cBuf.get();
if (Character.isLowSurrogate(low)) {
int codePoint = Character.toCodePoint(c, low);
if (Character.isValidCodePoint(codePoint)) {
iBuf.put(codePoint);
} else {
throw new UnsupportedOperationException("Handle me gracefully");
}
} else {
throw new UnsupportedOperationException("Handle me gracefully");
}
} else {
throw new UnsupportedOperationException("Handle me gracefully");
}
} else {
throw new UnsupportedOperationException("Handle me gracefully");
}
} else {
iBuf.put((int) c);
}
}
iBuf.flip();
int[] codePoints = new int[iBuf.limit()];
iBuf.get(codePoints);
onChar.accept(codePoints);
cBuf.compact();
if (result.isOverflow()) {
// We still have work to do
} else if (result.isUnderflow()) {
if (bBuf.hasRemaining()) {
// We need more input
Helper.noop();
} else {
// We are done
Helper.noop();
}
break;
} else {
throw new UnsupportedOperationException("Handle me gracefully");
}
}
bBuf.compact();
}
/**
* {@inheritDoc}
*/
@Override
public String getString(CharsetDecoder decoder) throws CharacterCodingException {
if (!hasRemaining()) {
return "";
}
boolean utf16 = decoder.charset().name().startsWith("UTF-16");
int oldPos = position();
int oldLimit = limit();
int end = -1;
int newPos;
if (!utf16) {
end = indexOf((byte) 0x00);
if (end < 0) {
newPos = end = oldLimit;
} else {
newPos = end + 1;
}
} else {
int i = oldPos;
for (;;) {
boolean wasZero = get(i) == 0;
i++;
if (i >= oldLimit) {
break;
}
if (get(i) != 0) {
i++;
if (i >= oldLimit) {
break;
}
continue;
}
if (wasZero) {
end = i - 1;
break;
}
}
if (end < 0) {
newPos = end = oldPos + (oldLimit - oldPos & 0xFFFFFFFE);
} else {
if (end + 2 <= oldLimit) {
newPos = end + 2;
} else {
newPos = end;
}
}
}
if (oldPos == end) {
position(newPos);
return "";
}
limit(end);
decoder.reset();
int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
CharBuffer out = CharBuffer.allocate(expectedLength);
for (;;) {
CoderResult cr;
if (hasRemaining()) {
cr = decoder.decode(buf(), out, true);
} else {
cr = decoder.flush(out);
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
out.flip();
o.put(out);
out = o;
continue;
}
if (cr.isError()) {
// Revert the buffer back to the previous state.
limit(oldLimit);
position(oldPos);
cr.throwException();
}
}
limit(oldLimit);
position(newPos);
return out.flip().toString();
}