use of java.nio.charset.CharacterCodingException in project buck by facebook.
the class UnixArchive method getLongFromStringAtRange.
private long getLongFromStringAtRange(int len, CharsetDecoder decoder) {
String filenameLengthString;
int offset = buffer.position();
try {
filenameLengthString = readStringWithLength(buffer, len, decoder);
} catch (CharacterCodingException e) {
throw new HumanReadableException(e, "Unable to read long from buffer (range %d..%d)", offset, offset + len);
}
return Long.parseLong(filenameLengthString.trim());
}
use of java.nio.charset.CharacterCodingException in project buck by facebook.
the class UnixArchive method loadEntries.
@SuppressWarnings("PMD.PrematureDeclaration")
private ImmutableList<UnixArchiveEntry> loadEntries() {
int offset = EXPECTED_GLOBAL_HEADER.length;
int headerOffset = offset;
ImmutableList.Builder<UnixArchiveEntry> builder = ImmutableList.builder();
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
while (true) {
buffer.position(offset);
byte[] markerBytes = new byte[ENTRY_MARKER.length];
buffer.get(markerBytes, 0, markerBytes.length);
if (!Arrays.equals(markerBytes, ENTRY_MARKER)) {
throw new HumanReadableException("Unknown entry marker");
}
int filenameLength = getIntFromStringAtRange(LENGTH_OF_FILENAME_SIZE, decoder);
long fileModification = getLongFromStringAtRange(MODIFICATION_TIME_SIZE, decoder);
int ownerId = getIntFromStringAtRange(OWNER_ID_SIZE, decoder);
int groupId = getIntFromStringAtRange(GROUP_ID_SIZE, decoder);
int fileMode = getIntFromStringAtRange(FILE_MODE_SIZE, decoder);
int fileAndFilenameSize = getIntFromStringAtRange(FILE_AND_FILENAME_SIZE, decoder);
byte[] magic = new byte[END_OF_HEADER_MAGIC_SIZE];
buffer.get(magic, 0, magic.length);
if (!Arrays.equals(magic, END_OF_HEADER_MAGIC)) {
throw new HumanReadableException("Unknown file magic");
}
long fileSizeWithoutFilename = fileAndFilenameSize - filenameLength;
offset = buffer.position();
String filename;
try {
filename = nulTerminatedCharsetDecoder.decodeString(buffer);
} catch (CharacterCodingException e) {
throw new HumanReadableException(e, "Unable to read filename from buffer starting at %d", offset);
}
offset += filenameLength;
builder.add(UnixArchiveEntry.of(filenameLength, fileModification, ownerId, groupId, fileMode, fileSizeWithoutFilename, filename, headerOffset, offset - headerOffset, offset));
offset += fileSizeWithoutFilename;
if (offset == buffer.capacity()) {
break;
}
}
return builder.build();
}
use of java.nio.charset.CharacterCodingException in project gitblit by gitblit.
the class StringUtils method decodeString.
/**
* Decodes a string by trying several charsets until one does not throw a
* coding exception. Last resort is to interpret as UTF-8 with illegal
* character substitution.
*
* @param content
* @param charsets optional
* @return a string
*/
public static String decodeString(byte[] content, String... charsets) {
Set<String> sets = new LinkedHashSet<String>();
if (!ArrayUtils.isEmpty(charsets)) {
sets.addAll(Arrays.asList(charsets));
}
String value = null;
sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
for (String charset : sets) {
try {
Charset cs = Charset.forName(charset);
CharsetDecoder decoder = cs.newDecoder();
CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
value = buffer.toString();
break;
} catch (CharacterCodingException e) {
// ignore and advance to the next charset
} catch (IllegalCharsetNameException e) {
// ignore illegal charset names
} catch (UnsupportedCharsetException e) {
// ignore unsupported charsets
}
}
if (value != null && value.startsWith("")) {
// strip UTF-8 BOM
return value.substring(1);
}
return value;
}
use of java.nio.charset.CharacterCodingException in project MusicDNA by harjot-oberai.
the class Mp4HdlrBox method processData.
public void processData() throws CannotReadException {
//Skip other flags
dataBuffer.position(dataBuffer.position() + VERSION_FLAG_LENGTH + OTHER_FLAG_LENGTH + RESERVED_FLAG_LENGTH);
CharsetDecoder decoder = Charset.forName("ISO-8859-1").newDecoder();
try {
handlerType = decoder.decode((ByteBuffer) dataBuffer.slice().limit(HANDLER_LENGTH)).toString();
} catch (CharacterCodingException cee) {
//Ignore
}
//To getFields human readable name
mediaDataType = mediaDataTypeMap.get(handlerType);
}
use of java.nio.charset.CharacterCodingException in project cassandra by apache.
the class DynamicCompositeType method validateComparator.
protected AbstractType<?> validateComparator(int i, ByteBuffer bb) throws MarshalException {
AbstractType<?> comparator = null;
if (bb.remaining() < 2)
throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
int header = ByteBufferUtil.readShortLength(bb);
if ((header & 0x8000) == 0) {
if (bb.remaining() < header)
throw new MarshalException("Not enough bytes to read comparator name of component " + i);
ByteBuffer value = ByteBufferUtil.readBytes(bb, header);
String valueStr = null;
try {
valueStr = ByteBufferUtil.string(value);
comparator = TypeParser.parse(valueStr);
} catch (CharacterCodingException ce) {
// ByteBufferUtil.string failed.
// Log it here and we'll further throw an exception below since comparator == null
logger.error("Failed with [{}] when decoding the byte buffer in ByteBufferUtil.string()", ce.toString());
} catch (Exception e) {
// parse failed.
// Log it here and we'll further throw an exception below since comparator == null
logger.error("Failed to parse value string \"{}\" with exception: [{}]", valueStr, e.toString());
}
} else {
comparator = aliases.get((byte) (header & 0xFF));
}
if (comparator == null)
throw new MarshalException("Cannot find comparator for component " + i);
else
return comparator;
}
Aggregations