@Override public String fromByteBuffer(final ByteBuffer buffer, final int numBytes) { return StringUtils.fromUtf8(buffer, numBytes); }
public static String fromUtf8(final ByteBuffer buffer, final int numBytes) { final byte[] bytes = new byte[numBytes]; buffer.get(bytes); return fromUtf8(bytes); }
@Nullable private String charsetFix(String s) { if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } }
@Nullable public static String decodeUserSecret(String encodedUserSecret) { try { return StringUtils.fromUtf8(StringUtils.decodeBase64String(encodedUserSecret)); } catch (IllegalArgumentException iae) { return null; } }
@Override public InventoryType deserializeInventory(byte[] bytes) { try { return jsonMapper.readValue(bytes, typeReference); } catch (IOException e) { log.error(e, "Could not parse json: %s", StringUtils.fromUtf8(bytes)); throw Throwables.propagate(e); } }
public String readString(ByteBuffer in) { final int length = in.getInt(); return StringUtils.fromUtf8(readBytes(in, length)); }
private static String readString(DataInput in) throws IOException { byte[] result = readBytes(in); return StringUtils.fromUtf8(result); }
private String extractEtagFromResults( final byte[] cachedResult ) { if (cachedResult == null) { return null; } log.debug("Fetching result level cache identifier for query: %s", query.getId()); int etagLength = ByteBuffer.wrap(cachedResult, 0, Integer.BYTES).getInt(); return StringUtils.fromUtf8(Arrays.copyOfRange(cachedResult, Integer.BYTES, etagLength + Integer.BYTES)); }
/** * Encode the serialized form generated by toBytes() in Base64, used as the JSON serialization format. * * @return Base64 serialization */ @JsonValue public String toBase64() { byte[] asBytes = toBytes(); return StringUtils.fromUtf8(StringUtils.encodeBase64(asBytes)); }
String readString(InputStream in) throws IOException { final int length = readInt(in); byte[] stringBytes = new byte[length]; ByteStreams.readFully(in, stringBytes); return StringUtils.fromUtf8(stringBytes); }
/** * Convert an input to base 64 and return the utf8 string of that byte array * * @param input The string to convert to base64 * @return the base64 of the input in string form */ public static String utf8Base64(String input) { return fromUtf8(encodeBase64(toUtf8(input))); }
@Test(expected = NullPointerException.class) public void testNullPointerByteBuffer() { StringUtils.fromUtf8((ByteBuffer) null); }
@Test(expected = BufferUnderflowException.class) public void testOutOfBounds() { ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'}); bytes.position(1).limit(3); StringUtils.fromUtf8(bytes, 3); }
@Test(expected = NullPointerException.class) public void testNullPointerByteArray() { StringUtils.fromUtf8((byte[]) null); }
private String readLog(TaskLogs taskLogs, String logFile, long offset) throws IOException { return StringUtils.fromUtf8(ByteStreams.toByteArray(taskLogs.streamTaskLog(logFile, offset).get().openStream())); } }
private void verifyContents(WriteOutBytes writeOutBytes, String expected) throws IOException { Assert.assertEquals(expected, IOUtils.toString(writeOutBytes.asInputStream(), StandardCharsets.US_ASCII)); ByteBuffer bb = ByteBuffer.allocate((int) writeOutBytes.size()); writeOutBytes.readFully(0, bb); bb.flip(); Assert.assertEquals(expected, StringUtils.fromUtf8(bb)); }