use of java.nio.charset.CoderResult in project Bytecoder by mirkosertic.
the class StringCoding method decode.
static Result decode(Charset cs, byte[] ba, int off, int len) {
// is started...
if (cs == UTF_8) {
return StringDecoderUTF8.decode(ba, off, len, new Result());
}
CharsetDecoder cd = cs.newDecoder();
// ascii fastpath
if (cs == ISO_8859_1 || ((cd instanceof ArrayDecoder) && ((ArrayDecoder) cd).isASCIICompatible() && !hasNegatives(ba, off, len))) {
if (COMPACT_STRINGS) {
return new Result().with(Arrays.copyOfRange(ba, off, off + len), LATIN1);
} else {
return new Result().with(StringLatin1.inflate(ba, off, len), UTF16);
}
}
int en = scale(len, cd.maxCharsPerByte());
if (len == 0) {
return new Result().with();
}
if (cs.getClass().getClassLoader0() != null && System.getSecurityManager() != null) {
ba = Arrays.copyOfRange(ba, off, off + len);
off = 0;
}
cd.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
char[] ca = new char[en];
if (cd instanceof ArrayDecoder) {
int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
return new Result().with(ca, 0, clen);
}
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) {
// so this shouldn't happen
throw new Error(x);
}
return new Result().with(ca, 0, cb.position());
}
use of java.nio.charset.CoderResult in project wigle-wifi-wardriving by wiglenet.
the class HttpFileUploader method writeString.
/**
* write a string out to a byte channel.
*
* @param wbc the byte channel to write to
* @param str the string to write
* @param enc the cbc encoder to use, will be reset
* @param cbuff the scratch charbuffer, will be cleared
* @param bbuff the scratch bytebuffer, will be cleared
*/
private static void writeString(WritableByteChannel wbc, String str, CharsetEncoder enc, CharBuffer cbuff, ByteBuffer bbuff) throws IOException {
// clear existing state
cbuff.clear();
bbuff.clear();
enc.reset();
cbuff.put(str);
cbuff.flip();
CoderResult result = enc.encode(cbuff, bbuff, true);
if (CoderResult.UNDERFLOW != result) {
throw new IOException("encode fail. result: " + result + " cbuff: " + cbuff + " bbuff: " + bbuff);
}
result = enc.flush(bbuff);
if (CoderResult.UNDERFLOW != result) {
throw new IOException("flush fail. result: " + result + " bbuff: " + bbuff);
}
bbuff.flip();
int remaining = bbuff.remaining();
while (remaining > 0) {
remaining -= wbc.write(bbuff);
}
}
use of java.nio.charset.CoderResult in project ant by apache.
the class NioZipEncoding method encode.
/**
* @see org.apache.tools.zip.ZipEncoding#encode(java.lang.String)
*/
public ByteBuffer encode(final String name) {
final CharsetEncoder enc = this.charset.newEncoder();
enc.onMalformedInput(CodingErrorAction.REPORT);
enc.onUnmappableCharacter(CodingErrorAction.REPORT);
final CharBuffer cb = CharBuffer.wrap(name);
ByteBuffer out = ByteBuffer.allocate(name.length() + (name.length() + 1) / 2);
while (cb.remaining() > 0) {
final CoderResult res = enc.encode(cb, out, true);
if (res.isUnmappable() || res.isMalformed()) {
// pseudo-URL encoding style to ByteBuffer.
if (res.length() * 6 > out.remaining()) {
out = ZipEncodingHelper.growBuffer(out, out.position() + res.length() * 6);
}
for (int i = 0; i < res.length(); ++i) {
ZipEncodingHelper.appendSurrogate(out, cb.get());
}
} else if (res.isOverflow()) {
out = ZipEncodingHelper.growBuffer(out, 0);
} else if (res.isUnderflow()) {
enc.flush(out);
break;
}
}
out.limit(out.position());
out.rewind();
return out;
}
use of java.nio.charset.CoderResult in project derby by apache.
the class DDMWriter method writeString.
/**
* Write string with default encoding
*
* @param s value to be written
*
* @exception DRDAProtocolException
*/
protected void writeString(String s) throws DRDAProtocolException {
ensureLength(maxEncodedLength(s));
CharBuffer input = CharBuffer.wrap(s);
encoder.reset();
CoderResult res = encoder.encode(input, buffer, true);
if (res == CoderResult.UNDERFLOW) {
res = encoder.flush(buffer);
}
if (SanityManager.DEBUG) {
SanityManager.ASSERT(res == CoderResult.UNDERFLOW, "CharBuffer was not exhausted: res = " + res);
}
}
use of java.nio.charset.CoderResult in project teiid by teiid.
the class ReaderInputStream method read.
@Override
public int read(byte[] bbuf, int off, int len) throws IOException {
if ((off < 0) || (off > bbuf.length) || (len < 0) || ((off + len) > bbuf.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
while (!done && !bb.hasRemaining()) {
int read = 0;
int pos = cb.position();
if (!wasOverflow) {
while ((read = reader.read(cb)) == 0) {
// blocking read
}
cb.flip();
}
bb.clear();
CoderResult cr = encoder.encode(cb, bb, read == -1);
checkResult(cr);
if (read == -1 && !wasOverflow) {
cr = encoder.flush(bb);
checkResult(cr);
if (!wasOverflow) {
done = true;
}
}
if (!wasOverflow) {
if (read != 0 && cb.position() != read + pos) {
cb.compact();
} else {
cb.clear();
}
}
bb.flip();
}
len = Math.min(len, bb.remaining());
if (len == 0 && done) {
return -1;
}
bb.get(bbuf, off, len);
return len;
}
Aggregations