@Override public String toString() { return "Funnels.stringFunnel(" + charset.name() + ")"; }
String content = readFile("test.txt", Charset.defaultCharset());
@Override public Charset convert(String source) { return Charset.forName(source); }
/** * Returns a Charset for the named charset. If the name is null, return the default Charset. * * @param charset * The name of the requested charset, may be null. * @return a Charset for the named charset * @throws java.nio.charset.UnsupportedCharsetException * If the named charset is unavailable */ public static Charset toCharset(final String charset) { return charset == null ? Charset.defaultCharset() : Charset.forName(charset); }
public WriterOutputStream(Writer out) { this.writer = out; decoder = DEFAULT_CHARSET.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); }
/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set. Malformed input and unmappable characters will be replaced. * * @param reader input source * @param charset character set used for encoding chars to bytes * @param bufferSize size of internal input and output buffers * @throws IllegalArgumentException if bufferSize is non-positive */ ReaderInputStream(Reader reader, Charset charset, int bufferSize) { this( reader, charset .newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
byte[] b = string.getBytes(); byte[] b = string.getBytes(Charset.forName("UTF-8")); byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only
/** * Creates a new instance with the current system character set. */ public StringEncoder() { this(Charset.defaultCharset()); }
SerializedForm(Charset charset) { this.charsetCanonicalName = charset.name(); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charset the charset encoding * @param bufferSize the size of the input buffer in number of characters */ public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { this(reader, charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
@Override public CharSource asCharSource(Charset charset) { if (charset.equals(this.charset)) { return CharSource.this; } return super.asCharSource(charset); }
/** * Constructor, calls {@link #CharSequenceInputStream(CharSequence, Charset, int)}. * * @param cs the input character sequence * @param charset the character set name to use * @param bufferSize the buffer size to use. * @throws IllegalArgumentException if the buffer is not large enough to hold a complete character */ public CharSequenceInputStream(final CharSequence cs, final String charset, final int bufferSize) { this(cs, Charset.forName(charset), bufferSize); }
/** * Creates a new instance with the current system character set. */ public StringDecoder() { this(Charset.defaultCharset()); }
@Override public boolean equals(@Nullable Object o) { if (o instanceof StringCharsetFunnel) { StringCharsetFunnel funnel = (StringCharsetFunnel) o; return this.charset.equals(funnel.charset); } return false; }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charsetName the name of the charset encoding * @param bufferSize the size of the input buffer in number of characters */ public ReaderInputStream(final Reader reader, final String charsetName, final int bufferSize) { this(reader, Charset.forName(charsetName), bufferSize); }
/** * Returns the given Charset or the default Charset if the given Charset is null. * * @param charset * A charset or null. * @return the given Charset or the default Charset if the given Charset is null */ public static Charset toCharset(final Charset charset) { return charset == null ? Charset.defaultCharset() : charset; }
List<String> lines = Arrays.asList("The first line", "The second line"); Path file = Paths.get("the-file-name.txt"); Files.write(file, lines, Charset.forName("UTF-8")); //Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
/** Returns the charset that should be used to encode the credentials. */ public Charset charset() { String charset = authParams.get("charset"); if (charset != null) { try { return Charset.forName(charset); } catch (Exception ignore) { } } return ISO_8859_1; }
/** * Returns the charset of this media type, or {@code defaultValue} if either this media type * doesn't specify a charset, of it its charset is unsupported by the current runtime. */ public @Nullable Charset charset(@Nullable Charset defaultValue) { try { return charset != null ? Charset.forName(charset) : defaultValue; } catch (IllegalArgumentException e) { return defaultValue; // This charset is invalid or unsupported. Give up. } }