use of java.io.UTFDataFormatException in project TinyKeePass by sorz.
the class SecureStringStorage method get.
@Nullable
public List<String> get(Cipher cipher) throws BadPaddingException, IllegalBlockSizeException {
String ciphertext = preferences.getString(PREF_VALUE_NAME, null);
if (ciphertext == null)
return null;
byte[] cleartext = cipher.doFinal(Base64.decode(ciphertext, Base64.DEFAULT));
DataInputStream input = new DataInputStream(new ByteArrayInputStream(cleartext));
List<String> strings = new ArrayList<>();
try {
// noinspection InfiniteLoopStatement
while (true) strings.add(input.readUTF());
} catch (EOFException e) {
return strings;
} catch (UTFDataFormatException e) {
Log.e(TAG, "fail to parse string", e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
use of java.io.UTFDataFormatException in project cassandra by apache.
the class UnbufferedDataOutputStreamPlus method writeUTF.
/**
* Writes the specified String out in UTF format to the provided DataOutput
*
* @param str the String to be written in UTF format.
* @param out the DataOutput to write the UTF encoded string to
* @throws IOException If an error occurs attempting to write to this
* DataOutputStream.
*/
public static void writeUTF(String str, DataOutput out) throws IOException {
int length = str.length();
if (length == 0) {
out.write(zeroBytes);
return;
}
int utfCount = 0;
int maxSize = 2;
for (int i = 0; i < length; i++) {
int ch = str.charAt(i);
if ((ch > 0) & (ch <= 127))
utfCount += 1;
else if (ch <= 2047)
utfCount += 2;
else
utfCount += maxSize = 3;
}
if (utfCount > 65535)
//$NON-NLS-1$
throw new UTFDataFormatException();
byte[] utfBytes = retrieveTemporaryBuffer(utfCount + 2);
int bufferLength = utfBytes.length;
if (utfCount == length) {
utfBytes[0] = (byte) (utfCount >> 8);
utfBytes[1] = (byte) utfCount;
int firstIndex = 2;
for (int offset = 0; offset < length; offset += bufferLength) {
int runLength = Math.min(bufferLength - firstIndex, length - offset) + firstIndex;
offset -= firstIndex;
for (int i = firstIndex; i < runLength; i++) utfBytes[i] = (byte) str.charAt(offset + i);
out.write(utfBytes, 0, runLength);
firstIndex = 0;
}
} else {
int utfIndex = 2;
int offset = 0;
utfBytes[0] = (byte) (utfCount >> 8);
utfBytes[1] = (byte) utfCount;
while (length > 0) {
int charRunLength = (utfBytes.length - utfIndex) / maxSize;
if (charRunLength < 128 && charRunLength < length) {
out.write(utfBytes, 0, utfIndex);
utfIndex = 0;
}
if (charRunLength > length)
charRunLength = length;
for (int i = 0; i < charRunLength; i++) {
char ch = str.charAt(offset + i);
if ((ch > 0) && (ch <= 127)) {
utfBytes[utfIndex++] = (byte) ch;
} else if (ch <= 2047) {
utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (ch >> 6)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
} else {
utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (ch >> 12)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (ch >> 6)));
utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
}
}
offset += charRunLength;
length -= charRunLength;
}
out.write(utfBytes, 0, utfIndex);
}
}
use of java.io.UTFDataFormatException in project atlas by alibaba.
the class Mutf8 method decode.
/**
* Decodes bytes from {@code in} into {@code out} until a delimiter 0x00 is
* encountered. Returns a new string containing the decoded characters.
*/
public static String decode(ByteInput in, char[] out) throws UTFDataFormatException {
int s = 0;
while (true) {
char a = (char) (in.readByte() & 0xff);
if (a == 0) {
return new String(out, 0, s);
}
out[s] = a;
if (a < '') {
s++;
} else if ((a & 0xe0) == 0xc0) {
int b = in.readByte() & 0xff;
if ((b & 0xC0) != 0x80) {
throw new UTFDataFormatException("bad second byte");
}
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
int b = in.readByte() & 0xff;
int c = in.readByte() & 0xff;
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
throw new UTFDataFormatException("bad second or third byte");
}
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else {
throw new UTFDataFormatException("bad byte");
}
}
}
use of java.io.UTFDataFormatException in project Cloud9 by lintool.
the class WritableComparatorUtils method readUTF.
public static String readUTF(byte[] bytes, int s) {
try {
int utflen = WritableComparator.readUnsignedShort(bytes, s);
byte[] bytearr = new byte[utflen];
char[] chararr = new char[utflen];
int c, char2, char3;
int count = 0;
int chararr_count = 0;
System.arraycopy(bytes, s + 2, bytearr, 0, utflen);
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
if (c > 127)
break;
count++;
chararr[chararr_count++] = (char) c;
}
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
switch(c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
/* 0xxxxxxx */
count++;
chararr[chararr_count++] = (char) c;
break;
case 12:
case 13:
/* 110x xxxx 10xx xxxx */
count += 2;
if (count > utflen)
throw new UTFDataFormatException("malformed input: partial character at end");
char2 = (int) bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
throw new UTFDataFormatException("malformed input around byte " + count);
chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
throw new UTFDataFormatException("malformed input: partial character at end");
char2 = (int) bytearr[count - 2];
char3 = (int) bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new UTFDataFormatException("malformed input around byte " + (count - 1));
chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
default:
/* 10xx xxxx, 1111 xxxx */
throw new UTFDataFormatException("malformed input around byte " + count);
}
}
// The number of chars produced may be less than utflen
return new String(chararr, 0, chararr_count);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of java.io.UTFDataFormatException in project tinker by Tencent.
the class Mutf8 method decode.
/**
* Decodes bytes from {@code in} into {@code out} until a delimiter 0x00 is
* encountered. Returns a new string containing the decoded characters.
*/
public static String decode(ByteInput in, char[] out) throws UTFDataFormatException {
int s = 0;
while (true) {
char a = (char) (in.readByte() & 0xff);
if (a == 0) {
return new String(out, 0, s);
}
out[s] = a;
if (a < '') {
s++;
} else if ((a & 0xe0) == 0xc0) {
int b = in.readByte() & 0xff;
if ((b & 0xC0) != 0x80) {
throw new UTFDataFormatException("bad second byte");
}
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
int b = in.readByte() & 0xff;
int c = in.readByte() & 0xff;
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
throw new UTFDataFormatException("bad second or third byte");
}
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else {
throw new UTFDataFormatException("bad byte");
}
}
}
Aggregations