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);
}
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);
}
}
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;
}
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());
}
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();
}
Aggregations