Java OutputStream Write writeStreamFromString(String contents, OutputStream outputStream)

Here you can find the source of writeStreamFromString(String contents, OutputStream outputStream)

Description

write Stream From String

License

Open Source License

Declaration

public static void writeStreamFromString(String contents, OutputStream outputStream) throws IOException 

Method Source Code

//package com.java2s;
/**/*www . j a  v  a  2s.c  om*/
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class Main {
    public static void writeStreamFromString(String contents, OutputStream outputStream) throws IOException {
        if (contents == null) {
            return;
        }

        char[] buffer = new char[0x10000];

        Reader in = new InputStreamReader(new ByteArrayInputStream(contents.getBytes("UTF-8"))); //$NON-NLS-1$

        Writer out = new OutputStreamWriter(outputStream, "UTF-8"); //$NON-NLS-1$

        int read;
        do {
            read = in.read(buffer, 0, buffer.length);

            if (read > 0) {
                out.write(buffer, 0, read);
            }
        } while (read >= 0);

        in.close();
        out.flush();
        out.close();
    }
}

Related

  1. writeStream(OutputStream outputStream, InputStream inputStream, byte[] buffer)
  2. writeStream(String content, String charset, OutputStream outputStream)
  3. writeStreamBuffered(final Reader in, final Writer out)
  4. writeStreamCharwise(final Reader in, final Writer out)
  5. writeStreamCharwiseLimited(final Reader in, final Writer out, final int len)
  6. writeStreamFromString(String serializePath)
  7. writeStreamInFile(File f, InputStream in)
  8. writeStreamText(OutputStream out, String text)
  9. writeStreamToArray(InputStream stream)