Search in sources :

Example 36 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project mkgmap by openstreetmap.

the class TYPFile method writeLabels.

private void writeLabels(ImgFileWriter in) {
    if (data.getIcons().isEmpty())
        return;
    SectionWriter writer = header.getLabels().makeSectionWriter(in);
    List<SortKey<TypIconSet>> keys = new ArrayList<SortKey<TypIconSet>>();
    Sort sort = data.getSort();
    for (TypIconSet icon : data.getIcons()) {
        String label = icon.getLabel();
        if (label != null) {
            SortKey<TypIconSet> key = sort.createSortKey(icon, label);
            keys.add(key);
        }
    }
    Collections.sort(keys);
    // Offset 0 is reserved to mean no label.
    writer.put((byte) 0);
    for (SortKey<TypIconSet> key : keys) {
        int off = writer.position();
        TypIconSet icon = key.getObject();
        int type = icon.getTypeForFile();
        String label = icon.getLabel();
        if (label != null) {
            CharBuffer cb = CharBuffer.wrap(label);
            CharsetEncoder encoder = data.getEncoder();
            try {
                ByteBuffer buffer = encoder.encode(cb);
                writer.put(buffer);
                // If we succeeded then note offsets for indexes
                strToType.put(off, type);
                typeToStr.put(type, off);
            } catch (CharacterCodingException ignore) {
                String name = encoder.charset().name();
                throw new TypLabelException(name);
            }
            writer.put((byte) 0);
        }
    }
    Utils.closeFile(writer);
}
Also used : SectionWriter(uk.me.parabola.imgfmt.app.SectionWriter) ArrayList(java.util.ArrayList) CharBuffer(java.nio.CharBuffer) SortKey(uk.me.parabola.imgfmt.app.srt.SortKey) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) Sort(uk.me.parabola.imgfmt.app.srt.Sort)

Example 37 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project mkgmap by openstreetmap.

the class Sort method createSortKey.

/**
 * Create a sort key for a given unicode string.  The sort key can be compared instead of the original strings
 * and will compare based on the sorting represented by this Sort class.
 *
 * Using a sort key is more efficient if many comparisons are being done (for example if you are sorting a
 * list of strings).
 *
 * @param object This is saved in the sort key for later retrieval and plays no part in the sorting.
 * @param s The string for which the sort key is to be created.
 * @param second Secondary sort key.
 * @param cache A cache for the created keys. This is for saving memory so it is essential that this
 * is managed by the caller.
 * @return A sort key.
 */
public <T> SortKey<T> createSortKey(T object, String s, int second, Map<String, byte[]> cache) {
    if (s.length() == 0)
        return new SrtSortKey<>(object, ZERO_KEY, second);
    // If there is a cache then look up and return the key.
    // This is primarily for memory management, not for speed.
    byte[] key;
    if (cache != null) {
        key = cache.get(s);
        if (key != null)
            return new SrtSortKey<>(object, key, second);
    }
    try {
        char[] chars;
        if (isMulti()) {
            chars = s.toCharArray();
        } else {
            ByteBuffer out = encoder.encode(CharBuffer.wrap(s));
            byte[] bval = out.array();
            chars = new char[bval.length];
            for (int i = 0; i < bval.length; i++) chars[i] = (char) (bval[i] & 0xff);
        }
        key = makeKey(chars);
        if (cache != null)
            cache.put(s, key);
        return new SrtSortKey<>(object, key, second);
    } catch (CharacterCodingException e) {
        return new SrtSortKey<>(object, ZERO_KEY);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 38 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project mkgmap by openstreetmap.

the class Sort method encode.

public char[] encode(String s) {
    char[] chars = null;
    try {
        if (isMulti()) {
            chars = s.toCharArray();
        } else {
            ByteBuffer out = encoder.encode(CharBuffer.wrap(s));
            byte[] bval = out.array();
            chars = new char[bval.length];
            for (int i = 0; i < bval.length; i++) chars[i] = (char) (bval[i] & 0xff);
        }
    } catch (CharacterCodingException e) {
    }
    return chars;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 39 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project spf4j by zolyfarkas.

the class Strings method decode.

@SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
public static String decode(final CharsetDecoder cd, final byte[] ba, final int off, final int len) {
    if (len == 0) {
        return "";
    }
    int en = (int) (len * (double) cd.maxCharsPerByte());
    char[] ca = TLScratch.getCharsTmp(en);
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return new String(ca, 0, clen);
    }
    cd.reset();
    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) {
        throw new UncheckedIOException(x);
    }
    return new String(ca, 0, cb.position());
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharBuffer(java.nio.CharBuffer) UncheckedIOException(java.io.UncheckedIOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 40 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project liferay-ide by liferay.

the class BndProperties method _convert.

private String _convert(byte[] buffer, Charset charset) throws IOException {
    CharsetDecoder decoder = charset.newDecoder();
    ByteBuffer bb = ByteBuffer.wrap(buffer);
    CharBuffer cb = CharBuffer.allocate(buffer.length * 4);
    CoderResult result = decoder.decode(bb, cb, true);
    if (!result.isError()) {
        return new String(cb.array(), 0, cb.position());
    }
    throw new CharacterCodingException();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)196 ByteBuffer (java.nio.ByteBuffer)114 CharBuffer (java.nio.CharBuffer)48 CharsetDecoder (java.nio.charset.CharsetDecoder)44 IOException (java.io.IOException)34 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CoderResult (java.nio.charset.CoderResult)30 Charset (java.nio.charset.Charset)27 InputStream (java.io.InputStream)9 Date (java.util.Date)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 Path (java.nio.file.Path)6 ParseException (java.text.ParseException)6 Test (org.junit.Test)6 CoreException (org.eclipse.core.runtime.CoreException)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 OutputStream (java.io.OutputStream)4