@Test public void adjustCharset() throws Exception { Table<String, Optional<Charset>, Charset> testCases = ImmutableTable.<String, Optional<Charset>, Charset>builder() .put("", Optional.of(US_ASCII), US_ASCII) .put("abcdef", Optional.of(US_ASCII), US_ASCII) .put("Fractions: " + decodeFromHex("BCBDBE", ISO_8859_1), Optional.of(US_ASCII), UTF_8) .put("abcdef", Optional.of(ISO_8859_1), ISO_8859_1) .put("abcdef", Optional.of(UTF_8), UTF_8) .put("abc \uD83C\uDCA1\uD83C\uDCA8\ud83c\udcd1\ud83c\udcd8\ud83c\udcd3", Optional.of(ISO_8859_1), UTF_8) .build(); testCases.cellSet().forEach(cell -> { String text = cell.getRowKey(); @Nullable Charset charset = cell.getColumnKey().orElse(null); Charset expected = cell.getValue(); Charset actual = Hars.adjustCharset(text, charset, UTF_8); String description = String.format("\"%s\" with suggestion %s", StringEscapeUtils.escapeJava(text), charset); System.out.format("%s -> %s (expecting %s)%n", description, actual, expected); assertEquals(description, expected, actual); }); }
static Charset adjustCharset(String text, @Nullable Charset charset, Charset defaultCharset) { requireNonNull(defaultCharset, "default charset"); text = Strings.nullToEmpty(text); if (charset == null) { return defaultCharset; } CharsetEncoder encoder = charset.newEncoder(); if (encoder.canEncode(text)) { return charset; } return StandardCharsets.UTF_8; }
Hars { static Charset adjustCharset(String text, @Nullable Charset charset, Charset defaultCharset) { requireNonNull(defaultCharset, "default charset"); text = Strings.nullToEmpty(text); if (charset == null) { return defaultCharset; } CharsetEncoder encoder = charset.newEncoder(); if (encoder.canEncode(text)) { return charset; } return StandardCharsets.UTF_8; } }
Hars { static Charset adjustCharset(String text, @Nullable Charset charset, Charset defaultCharset) { requireNonNull(defaultCharset, "default charset"); text = Strings.nullToEmpty(text); if (charset == null) { return defaultCharset; } CharsetEncoder encoder = charset.newEncoder(); if (encoder.canEncode(text)) { return charset; } return StandardCharsets.UTF_8; }  }
Hars { static Charset adjustCharset(String text, @Nullable Charset charset, Charset defaultCharset) { requireNonNull(defaultCharset, "default charset"); text = Strings.nullToEmpty(text); if (charset == null) { return defaultCharset; } CharsetEncoder encoder = charset.newEncoder(); if (encoder.canEncode(text)) { return charset; } return StandardCharsets.UTF_8; }  static boolean isBase64Encoded(String contentType, String text, @Nullable String harContentEncoding, @Nullable Long bodySize); @Nullable static ByteSource getRequestPostData(@Nullable List<NameValuePair> params, String contentType, String postDataText, @Nullable Long requestBodySize, @Nullable String postDataComment, Charset defaultCharset); static TypedContent translateResponseContent(              String contentType,
                                                                      @Nullable String text,
                                                                      @Nullable Long bodySize,
                                                                      @Nullable Long contentSize,
                                                                      @Nullable String contentEncodingHeaderValue,
                                                                      @Nullable String harContentEncoding,
                                                                      @SuppressWarnings("unused") @Nullable String comment,
                                                                      Charset defaultCharset); static HarHeader newHarHeader(String name, String value); }
Hars { static Charset adjustCharset(String text, @Nullable Charset charset, Charset defaultCharset) { requireNonNull(defaultCharset, "default charset"); text = Strings.nullToEmpty(text); if (charset == null) { return defaultCharset; } CharsetEncoder encoder = charset.newEncoder(); if (encoder.canEncode(text)) { return charset; } return StandardCharsets.UTF_8; }  static boolean isBase64Encoded(String contentType, String text, @Nullable String harContentEncoding, @Nullable Long bodySize); @Nullable static ByteSource getRequestPostData(@Nullable List<NameValuePair> params, String contentType, String postDataText, @Nullable Long requestBodySize, @Nullable String postDataComment, Charset defaultCharset); static TypedContent translateResponseContent(              String contentType,
                                                                      @Nullable String text,
                                                                      @Nullable Long bodySize,
                                                                      @Nullable Long contentSize,
                                                                      @Nullable String contentEncodingHeaderValue,
                                                                      @Nullable String harContentEncoding,
                                                                      @SuppressWarnings("unused") @Nullable String comment,
                                                                      Charset defaultCharset); static HarHeader newHarHeader(String name, String value);  }