Java OutputStream Write String writeString(OutputStream out, String str)

Here you can find the source of writeString(OutputStream out, String str)

Description

Write a string (not null-terminated).

License

Open Source License

Parameter

Parameter Description
out The OutputStream to write the string to
str The string to write

Exception

Parameter Description
IOException an exception

Declaration

public static void writeString(OutputStream out, String str) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.OutputStream;
import java.util.Arrays;

public class Main {
    private static final byte[] buffer = new byte[1024];

    /**/*w ww  . ja  v a 2  s.  c  om*/
     * Write a string (not null-terminated).
     *
     * @param out The OutputStream to write the string to
     * @param str The string to write
     * @throws IOException
     */
    public static void writeString(OutputStream out, String str) throws IOException {
        out.write(str.getBytes());
    }

    /**
     * Write a number of bytes
     *
     * @param out  The OutputStream to write to
     * @param data The bytes to write
     * @throws IOException
     */
    public static void write(OutputStream out, byte[] data) throws IOException {
        out.write(data);
    }

    /**
     * Write a repeated byte value.
     *
     * @param out   The OutputStream to write to
     * @param value The value to write
     * @param count Number of times to write the value
     */
    public static void write(final OutputStream out, final int value, final int count) throws IOException {
        if (count > buffer.length) {
            // Fallback
            for (int i = 0; i < count; i++) {
                writeByte(out, value);
            }
        } else {
            Arrays.fill(buffer, (byte) value);
            out.write(buffer, 0, count);
        }
    }

    /**
     * Write a byte.
     *
     * @param out   The OutputStream to write the byte to
     * @param value The byte to write
     * @throws IOException
     */
    public static void writeByte(OutputStream out, int value) throws IOException {
        out.write(value);
    }
}

Related

  1. writeString(OutputStream out, String s)
  2. writeString(OutputStream out, String s)
  3. writeString(OutputStream out, String s)
  4. writeString(OutputStream out, String s)
  5. writeString(OutputStream out, String s)
  6. writeString(OutputStream out, String str)
  7. WriteString(OutputStream output, String string)
  8. writeString(OutputStream output, String string)
  9. writeString(String data, int length, OutputStream out)