Search in sources :

Example 51 with CharacterCodingException

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());
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 52 with CharacterCodingException

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ImmutableList(com.google.common.collect.ImmutableList) HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 53 with CharacterCodingException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 54 with CharacterCodingException

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);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 55 with CharacterCodingException

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;
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) SyntaxException(org.apache.cassandra.exceptions.SyntaxException) MarshalException(org.apache.cassandra.serializers.MarshalException)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)90 ByteBuffer (java.nio.ByteBuffer)46 CharsetEncoder (java.nio.charset.CharsetEncoder)16 CharBuffer (java.nio.CharBuffer)15 IOException (java.io.IOException)14 CoderResult (java.nio.charset.CoderResult)13 CharsetDecoder (java.nio.charset.CharsetDecoder)11 Charset (java.nio.charset.Charset)10 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 CoreException (org.eclipse.core.runtime.CoreException)5 IStatus (org.eclipse.core.runtime.IStatus)5 Status (org.eclipse.core.runtime.Status)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 SequenceInputStream (java.io.SequenceInputStream)4 Path (java.nio.file.Path)4 Date (java.util.Date)4