if (ok) {
return;
}
createEncoder();
}
final CharBuffer cb;
if (underflow == null) {
cb = input;
} else {
char[] newArray = new char[underflow.length + input.remaining()];
System.arraycopy(underflow, 0, newArray, 0, underflow.length);
input.get(newArray, underflow.length, input.remaining());
cb = CharBuffer.wrap(newArray);
underflow = null;
}
int last = -1;
while (cb.hasRemaining()) {
int remaining = buffer.remaining();
CoderResult result = charsetEncoder.encode(cb, buffer, false);
outputStream.updateWritten(remaining - buffer.remaining());
if (result.isOverflow() || !buffer.hasRemaining()) {
outputStream.flushInternal();
if (!buffer.hasRemaining()) {
error = true;
return;
}
}
if (result.isUnderflow()) {
underflow = new char[cb.remaining()];
cb.get(underflow);
return;
}
if (result.isError()) {
error = true;
return;
}
if (result.isUnmappable()) {
//this should not happen
error = true;
return;
}
if (last == cb.remaining()) {
underflow = new char[cb.remaining()];
cb.get(underflow);
return;
}
last = cb.remaining();
}
} catch (IOException e) {
error = true;
}
}