use of java.nio.charset.CoderResult in project VirtualXposed by android-hacker.
the class FastXmlSerializer method flush.
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;
}
}
use of java.nio.charset.CoderResult in project UniversalMediaServer by UniversalMediaServer.
the class FixedCharArrayByReference method getString.
/**
* Constructs a new {@code String} by decoding the referenced char array
* using {@code charset}.
*
* @param charset the {@link Charset} to use for decoding.
* @return The {@link String}.
*/
public String getString(Charset charset) {
if (size > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Array to big, please read it \"manually\" using getPointer.getX");
}
if (getPointer() == null) {
return null;
}
if (charset == null) {
charset = Charset.defaultCharset();
}
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
decoder.replaceWith("?");
ByteBuffer byteBuffer = ByteBuffer.wrap(getPointer().getByteArray(0, (int) size));
CharBuffer charBuffer = CharBuffer.allocate(100);
StringBuilder sb = new StringBuilder(100);
CoderResult coderResult = null;
while (coderResult == null || !coderResult.isUnderflow()) {
coderResult = decoder.decode(byteBuffer, charBuffer, true);
if (coderResult.isOverflow() && drainCharBuffer(charBuffer, sb)) {
return sb.toString();
}
}
if (drainCharBuffer(charBuffer, sb)) {
return sb.toString();
}
coderResult = null;
while (coderResult == null || !coderResult.isUnderflow()) {
coderResult = decoder.flush(charBuffer);
if (coderResult.isOverflow() && drainCharBuffer(charBuffer, sb)) {
return sb.toString();
}
}
if (drainCharBuffer(charBuffer, sb)) {
return sb.toString();
}
return sb.toString();
}
use of java.nio.charset.CoderResult in project Bytecoder by mirkosertic.
the class ParseUtil method decode.
/**
* Returns a new String constructed from the specified String by replacing
* the URL escape sequences and UTF8 encoding with the characters they
* represent.
*/
public static String decode(String s) {
int n = s.length();
if ((n == 0) || (s.indexOf('%') < 0))
return s;
StringBuilder sb = new StringBuilder(n);
ByteBuffer bb = ByteBuffer.allocate(n);
CharBuffer cb = CharBuffer.allocate(n);
CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8").onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
char c = s.charAt(0);
for (int i = 0; i < n; ) {
assert c == s.charAt(i);
if (c != '%') {
sb.append(c);
if (++i >= n)
break;
c = s.charAt(i);
continue;
}
bb.clear();
int ui = i;
for (; ; ) {
assert (n - i >= 2);
try {
bb.put(unescape(s, i));
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 3;
if (i >= n)
break;
c = s.charAt(i);
if (c != '%')
break;
}
bb.flip();
cb.clear();
dec.reset();
CoderResult cr = dec.decode(bb, cb, true);
if (cr.isError())
throw new IllegalArgumentException("Error decoding percent encoded characters");
cr = dec.flush(cb);
if (cr.isError())
throw new IllegalArgumentException("Error decoding percent encoded characters");
sb.append(cb.flip().toString());
}
return sb.toString();
}
use of java.nio.charset.CoderResult in project Bytecoder by mirkosertic.
the class ZipCoder method getBytes.
byte[] getBytes(String s) {
CharsetEncoder ce = encoder().reset();
char[] ca = s.toCharArray();
int len = (int) (ca.length * ce.maxBytesPerChar());
byte[] ba = new byte[len];
if (len == 0)
return ba;
// CodingErrorAction.REPLACE mode.
if (isUTF8 && ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder) ce).encode(ca, 0, ca.length, ba);
if (// malformed
blen == -1)
throw new IllegalArgumentException("MALFORMED");
return Arrays.copyOf(ba, blen);
}
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = ce.flush(bb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
if (// defensive copy?
bb.position() == ba.length)
return ba;
else
return Arrays.copyOf(ba, bb.position());
}
use of java.nio.charset.CoderResult in project Bytecoder by mirkosertic.
the class ZipCoder method toString.
String toString(byte[] ba, int off, int length) {
CharsetDecoder cd = decoder().reset();
int len = (int) (length * cd.maxCharsPerByte());
char[] ca = new char[len];
if (len == 0)
return new String(ca);
// REPORT mode.
if (isUTF8 && cd instanceof ArrayDecoder) {
int clen = ((ArrayDecoder) cd).decode(ba, off, length, ca);
if (// malformed
clen == -1)
throw new IllegalArgumentException("MALFORMED");
return new String(ca, 0, clen);
}
ByteBuffer bb = ByteBuffer.wrap(ba, off, 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());
}
Aggregations