List of usage examples for java.nio.charset CodingErrorAction REPLACE
CodingErrorAction REPLACE
To view the source code for java.nio.charset CodingErrorAction REPLACE.
Click Source Link
From source file:sdf_manager.ExporterSiteHTML.java
/** * * @param fieldName// w w w .ja v a2s .com * @return */ private String getString(String fieldName) { try { byte[] result = fieldName.getBytes(); //String tmp = result != null ? new String(result) : null; if (result != null && result.length == 0) { //log("Empty string for: " + fieldName); return null; } //don't enter empty string in the database else { if (result != null) { Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer cbuf = decoder.decode(ByteBuffer.wrap(result)); return cbuf.toString().trim(); } else { return null; } } } catch (Exception e) { logToFile("Failed extracting field: " + fieldName + ". The field could have an erroneous name. Please verify."); //e.printStackTrace(); ExporterSiteHTML.log.error("Failed extracting field: " + fieldName + ". The field could have an erroneous name. Please verify."); return null; } }
From source file:io.warp10.continuum.gts.GTSHelper.java
public static final long classId(long k0, long k1, String name) { CharsetEncoder ce = Charsets.UTF_8.newEncoder(); ce.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset(); byte[] ba = new byte[(int) ((double) ce.maxBytesPerChar() * name.length())]; int blen = ((ArrayEncoder) ce).encode(UnsafeString.getChars(name), UnsafeString.getOffset(name), name.length(), ba);/* ww w . j ava2s . c o m*/ return SipHashInline.hash24_palindromic(k0, k1, ba, 0, blen); }
From source file:io.warp10.continuum.gts.GTSHelper.java
public static final long labelsId(long sipkey0, long sipkey1, Map<String, String> labels) { ///*from ww w . ja v a 2 s. c om*/ // Allocate a CharsetEncoder. // Implementation is a sun.nio.cs.UTF_8$Encoder which implements ArrayEncoder // CharsetEncoder ce = Charsets.UTF_8.newEncoder(); // // Allocate arrays large enough for most cases // int calen = 64; byte[] ba = new byte[(int) ((double) ce.maxBytesPerChar() * calen)]; //char[] ca = new char[64]; // // Allocate an array to hold both name and value hashes // long[] hashes = new long[labels.size() * 2]; // // Compute hash for each name and each value // int idx = 0; for (Entry<String, String> entry : labels.entrySet()) { String ekey = entry.getKey(); String eval = entry.getValue(); int klen = ekey.length(); int vlen = eval.length(); if (klen > calen || vlen > calen) { //ca = new char[Math.max(klen,vlen)]; calen = Math.max(klen, vlen); ba = new byte[(int) ((double) ce.maxBytesPerChar() * calen)]; } //ekey.getChars(0, klen, ca, 0); ce.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset(); //int blen = ((ArrayEncoder)ce).encode(ca, 0, klen, ba); int blen = ((ArrayEncoder) ce).encode(UnsafeString.getChars(ekey), UnsafeString.getOffset(ekey), klen, ba); hashes[idx] = SipHashInline.hash24_palindromic(sipkey0, sipkey1, ba, 0, blen); //eval.getChars(0, vlen, ca, 0); ce.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset(); //blen = ((ArrayEncoder)ce).encode(ca, 0, vlen, ba); blen = ((ArrayEncoder) ce).encode(UnsafeString.getChars(eval), UnsafeString.getOffset(eval), vlen, ba); hashes[idx + 1] = SipHashInline.hash24_palindromic(sipkey0, sipkey1, ba, 0, blen); idx += 2; } // // Sort array by label hash then value hash. // Given the normally reduced number of labels, we can use a simple bubble sort... // for (int i = 0; i < labels.size() - 1; i++) { for (int j = i + 1; j < labels.size(); j++) { if (hashes[i * 2] > hashes[j * 2]) { // // We need to swap name and value ids at i and j // long tmp = hashes[j * 2]; hashes[j * 2] = hashes[i * 2]; hashes[i * 2] = tmp; tmp = hashes[j * 2 + 1]; hashes[j * 2 + 1] = hashes[i * 2 + 1]; hashes[i * 2 + 1] = tmp; } else if (hashes[i * 2] == hashes[j * 2] && hashes[i * 2 + 1] > hashes[j * 2 + 1]) { // // We need to swap value ids at i and j // long tmp = hashes[j * 2 + 1]; hashes[j * 2 + 1] = hashes[i * 2 + 1]; hashes[i * 2 + 1] = tmp; } } } // // Now compute the SipHash of all the longs in the order we just determined // byte[] buf = new byte[hashes.length * 8]; //ByteBuffer bb = ByteBuffer.wrap(buf); //bb.order(ByteOrder.BIG_ENDIAN); idx = 0; for (long hash : hashes) { buf[idx++] = (byte) ((hash >> 56) & 0xff); buf[idx++] = (byte) ((hash >> 48) & 0xff); buf[idx++] = (byte) ((hash >> 40) & 0xff); buf[idx++] = (byte) ((hash >> 32) & 0xff); buf[idx++] = (byte) ((hash >> 24) & 0xff); buf[idx++] = (byte) ((hash >> 16) & 0xff); buf[idx++] = (byte) ((hash >> 8) & 0xff); buf[idx++] = (byte) (hash & 0xff); //bb.putLong(hash); } //return SipHashInline.hash24(sipkey[0], sipkey[1], buf, 0, buf.length); return SipHashInline.hash24_palindromic(sipkey0, sipkey1, buf, 0, buf.length); }