Example usage for com.google.common.io CharSink CharSink

List of usage examples for com.google.common.io CharSink CharSink

Introduction

In this page you can find the example usage for com.google.common.io CharSink CharSink.

Prototype

CharSink

Source Link

Usage

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new char sink for the specified output stream.
 *
 * @since 1.3//from   w ww  . jav  a  2 s  .c  om
 * @param outputStream output stream, must not be null
 * @return a new char sink for the specified output stream
 */
public static CharSink outputStreamCharSink(final OutputStream outputStream) {
    checkNotNull(outputStream);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(new OutputStreamWriter(outputStream));
        }
    };
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new block compressed gzip (BGZF) char sink for the specified output stream.
 *
 * @since 1.3// w w w  .  j  a va  2s  .c o  m
 * @param outputStream output stream, must not be null
 * @return a new block compressed gzip (BGZF) char sink for the specified output stream
 */
public static CharSink bgzfOutputStreamCharSink(final OutputStream outputStream) {
    checkNotNull(outputStream);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(
                    new OutputStreamWriter(new BlockCompressedOutputStream(outputStream, (File) null)));
        }
    };
}

From source file:com.google.googlejavaformat.java.filer.FormattingJavaFileObject.java

@Override
public Writer openWriter() throws IOException {
    final StringBuilder stringBuilder = new StringBuilder(DEFAULT_FILE_SIZE);
    return new Writer() {
        @Override/*from   w  w w . j  a  va  2s  . c o  m*/
        public void write(char[] chars, int start, int end) throws IOException {
            stringBuilder.append(chars, start, end - start);
        }

        @Override
        public void write(String string) throws IOException {
            stringBuilder.append(string);
        }

        @Override
        public void flush() throws IOException {
        }

        @Override
        public void close() throws IOException {
            try {
                formatter.formatSource(CharSource.wrap(stringBuilder), new CharSink() {
                    @Override
                    public Writer openStream() throws IOException {
                        return fileObject.openWriter();
                    }
                });
            } catch (FormatterException e) {
                // An exception will happen when the code being formatted has an error. It's better to
                // log the exception and emit unformatted code so the developer can view the code which
                // caused a problem.
                try (Writer writer = fileObject.openWriter()) {
                    writer.append(stringBuilder.toString());
                }
                if (messager != null) {
                    messager.printMessage(Diagnostic.Kind.NOTE, "Error formatting " + getName());
                }
            }
        }
    };
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new gzip compressed char sink for the specified output stream.
 *
 * @param outputStream output stream, must not be null
 * @return a new gzip compressed char sink for the specified output stream
 *///  w ww. ja  v a2s .  co m
public static CharSink gzipOutputStreamCharSink(final OutputStream outputStream) {
    checkNotNull(outputStream);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(new OutputStreamWriter(new GzipCompressorOutputStream(outputStream)));
        }
    };
}

From source file:dagger.internal.codegen.JavaPoetSourceFileGenerator.java

/** Generates a source file to be compiled for {@code T}. */
void generate(T input) throws SourceFileGenerationException {
    ClassName generatedTypeName = nameGeneratedType(input);
    try {// w  ww.  j  a v  a2 s . c  o m
        Optional<TypeSpec.Builder> type = write(generatedTypeName, input);
        if (!type.isPresent()) {
            return;
        }
        JavaFile javaFile = buildJavaFile(generatedTypeName, type.get());

        final JavaFileObject sourceFile = filer.createSourceFile(generatedTypeName.toString(),
                Iterables.toArray(javaFile.typeSpec.originatingElements, Element.class));
        try {
            new Formatter().formatSource(CharSource.wrap(javaFile.toString()), new CharSink() {
                @Override
                public Writer openStream() throws IOException {
                    return sourceFile.openWriter();
                }
            });
        } catch (FormatterException e) {
            throw new SourceFileGenerationException(Optional.of(generatedTypeName), e,
                    getElementForErrorReporting(input));
        }
    } catch (Exception e) {
        // if the code above threw a SFGE, use that
        Throwables.propagateIfPossible(e, SourceFileGenerationException.class);
        // otherwise, throw a new one
        throw new SourceFileGenerationException(Optional.<ClassName>absent(), e,
                getElementForErrorReporting(input));
    }
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new bzip2 compressed char sink for the specified output stream.
 *
 * @param outputStream output stream, must not be null
 * @return a new bzip2 compressed char sink for the specified output stream
 */// w w  w  . ja v  a 2  s. c om
public static CharSink bzip2OutputStreamCharSink(final OutputStream outputStream) {
    checkNotNull(outputStream);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(new OutputStreamWriter(new BZip2CompressorOutputStream(outputStream)));
        }
    };
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new block compressed gzip (BGZF) char sink for the specified file.
 *
 * @since 1.2//from   w w  w .  ja va  2  s .c  om
 * @param file file, must not be null
 * @return a new block compressed gzip (BGZF) char sink for the specified file
 */
public static CharSink bgzfFileCharSink(final File file) {
    checkNotNull(file);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(new OutputStreamWriter(new BlockCompressedOutputStream(file)));
        }
    };
}

From source file:org.obiba.opal.core.upgrade.v2_0_x.HashShiroIniPasswordUpgradeStep.java

private void backupAndWriteIniFile() throws IOException {
    Files.copy(srcIniFile, new File(srcIniFile.getAbsolutePath() + ".opal1-backup"));
    CharSink charSink = new CharSink() {
        @Override// w w w .  j av a2  s.c  om
        public Writer openStream() throws IOException {
            return new FileWriter(destIniFile);
        }
    };
    charSink.writeLines(lines);
}

From source file:org.dishevelled.compress.Sinks.java

/**
 * Create and return a new gzip compressed char sink for the specified file.
 *
 * @since 1.3/*from w  ww  .ja va  2  s  . co m*/
 * @param file file, must not be null
 * @param append true to append to the specified file
 * @return a new gzip compressed char sink for the specified file
 */
public static CharSink gzipFileCharSink(final File file, final boolean append) {
    checkNotNull(file);
    return new CharSink() {
        @Override
        public Writer openStream() throws IOException {
            return new BufferedWriter(
                    new OutputStreamWriter(new GzipCompressorOutputStream(new FileOutputStream(file, append))));
        }
    };
}

From source file:com.lyndir.masterpassword.model.impl.MPFileUserManager.java

/**
 * Write the current user state to disk.
 *///from w w  w.  ja va 2  s . c  o  m
public void save(final MPFileUser user, final MPMasterKey masterKey)
        throws MPKeyUnavailableException, MPAlgorithmException {
    try {
        MPMarshalFormat format = user.getFormat();
        new CharSink() {
            @Override
            public Writer openStream() throws IOException {
                return new OutputStreamWriter(new FileOutputStream(getUserFile(user, format)), Charsets.UTF_8);
            }
        }.write(format.marshaller().marshall(user));
    } catch (final MPMarshalException | IOException e) {
        logger.err(e, "Unable to save sites for user: %s", user);
    }
}