use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.
the class Files method newBufferedWriter.
/**
* Opens or creates a file for writing, returning a {@code BufferedWriter}
* that may be used to write text to the file in an efficient manner.
* The {@code options} parameter specifies how the the file is created or
* opened. If no options are present then this method works as if the {@link
* StandardOpenOption#CREATE CREATE}, {@link
* StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
* StandardOpenOption#WRITE WRITE} options are present. In other words, it
* opens the file for writing, creating the file if it doesn't exist, or
* initially truncating an existing {@link #isRegularFile regular-file} to
* a size of {@code 0} if it exists.
*
* <p> The {@code Writer} methods to write text throw {@code IOException}
* if the text cannot be encoded using the specified charset.
*
* @param path
* the path to the file
* @param cs
* the charset to use for encoding
* @param options
* options specifying how the file is opened
*
* @return a new buffered writer, with default buffer size, to write text
* to the file
*
* @throws IOException
* if an I/O error occurs opening or creating the file
* @throws UnsupportedOperationException
* if an unsupported option is specified
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file.
*
* @see #write(Path,Iterable,Charset,OpenOption[])
*/
public static BufferedWriter newBufferedWriter(Path path, Charset cs, OpenOption... options) throws IOException {
CharsetEncoder encoder = cs.newEncoder();
Writer writer = new OutputStreamWriter(newOutputStream(path, options), encoder);
return new BufferedWriter(writer);
}
use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.
the class Files method write.
/**
* Write lines of text to a file. Each line is a char sequence and is
* written to the file in sequence with each line terminated by the
* platform's line separator, as defined by the system property {@code
* line.separator}. Characters are encoded into bytes using the specified
* charset.
*
* <p> The {@code options} parameter specifies how the the file is created
* or opened. If no options are present then this method works as if the
* {@link StandardOpenOption#CREATE CREATE}, {@link
* StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
* StandardOpenOption#WRITE WRITE} options are present. In other words, it
* opens the file for writing, creating the file if it doesn't exist, or
* initially truncating an existing {@link #isRegularFile regular-file} to
* a size of {@code 0}. The method ensures that the file is closed when all
* lines have been written (or an I/O error or other runtime exception is
* thrown). If an I/O error occurs then it may do so after the file has
* created or truncated, or after some bytes have been written to the file.
*
* @param path
* the path to the file
* @param lines
* an object to iterate over the char sequences
* @param cs
* the charset to use for encoding
* @param options
* options specifying how the file is opened
*
* @return the path
*
* @throws IOException
* if an I/O error occurs writing to or creating the file, or the
* text cannot be encoded using the specified charset
* @throws UnsupportedOperationException
* if an unsupported option is specified
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file.
*/
public static Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) throws IOException {
// ensure lines is not null before opening file
Objects.requireNonNull(lines);
CharsetEncoder encoder = cs.newEncoder();
OutputStream out = newOutputStream(path, options);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
for (CharSequence line : lines) {
writer.append(line);
writer.newLine();
}
}
return path;
}
use of java.nio.charset.CharsetEncoder in project voltdb by VoltDB.
the class CharsetUtil method encoder.
/**
* Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}.
*
* @param charset The specified charset
* @return The encoder for the specified <code>charset</code>
*/
public static CharsetEncoder encoder(Charset charset) {
checkNotNull(charset, "charset");
Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
CharsetEncoder e = map.get(charset);
if (e != null) {
e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
return e;
}
e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
map.put(charset, e);
return e;
}
use of java.nio.charset.CharsetEncoder in project android_frameworks_base by crdroidandroid.
the class StrictJarManifest method write.
/**
* Writes out the attribute information of the specified manifest to the
* specified {@code OutputStream}
*
* @param manifest
* the manifest to write out.
* @param out
* The {@code OutputStream} to write to.
* @throws IOException
* If an error occurs writing the {@code StrictJarManifest}.
*/
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
String version = manifest.mainAttributes.getValue(versionName);
if (version == null) {
versionName = Attributes.Name.SIGNATURE_VERSION;
version = manifest.mainAttributes.getValue(versionName);
}
if (version != null) {
writeEntry(out, versionName, version, encoder, buffer);
Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
if (!name.equals(versionName)) {
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
}
}
}
out.write(LINE_SEPARATOR);
Iterator<String> i = manifest.getEntries().keySet().iterator();
while (i.hasNext()) {
String key = i.next();
writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
Attributes attributes = manifest.entries.get(key);
Iterator<?> entries = attributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
writeEntry(out, name, attributes.getValue(name), encoder, buffer);
}
out.write(LINE_SEPARATOR);
}
}
use of java.nio.charset.CharsetEncoder in project xtext-xtend by eclipse.
the class UnicodeAwarePostProcessor method postProcess.
@Override
public CharSequence postProcess(URI fileURI, CharSequence content, /* @Nullable */
Charset targetCharset) {
if (targetCharset != null && "java".equalsIgnoreCase(fileURI.fileExtension())) {
final String lineSeparator = getWhitespaceInformationProvider().getLineSeparatorInformation(fileURI).getLineSeparator();
final CharsetEncoder encoder = targetCharset.newEncoder();
if (content instanceof TreeAppendable) {
return ((TreeAppendable) content).acceptVisitor(new TreeAppendable.Visitor() {
@Override
protected /* @NonNull */
String visit(/* @NonNull */
String string) {
return replaceLineSeparatorsAndEscapeChars(string, lineSeparator, encoder);
}
});
} else {
String result = replaceLineSeparatorsAndEscapeChars(content, lineSeparator, encoder);
return result;
}
} else {
return postProcess(fileURI, content);
}
}
Aggregations