Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

In this page you can find the example usage for java.io Writer write.

Prototype

public void write(String str, int off, int len) throws IOException 

Source Link

Document

Writes a portion of a string.

Usage

From source file:Base64.java

/** Returns an {@link OutputStream}, that encodes its input in Base64
 * and writes it to the given {@link Writer}. If the Base64 stream
 * ends, then the output streams {@link OutputStream#close()} method
 * must be invoked. Note, that this will <em>not</em> close the
 * target {@link Writer}!/*  w  w  w.j  a v a  2s.  c om*/
 * @param pWriter Target writer.
 * @param pLineSize Size of one line in characters, must be a multiple
 * of four. Zero indicates, that no line wrapping should occur.
 * @param pSeparator Line separator or null, in which case the default value
 * {@link #LINE_SEPARATOR} is used.
 * @return An output stream, encoding its input in Base64 and writing
 * the output to the writer <code>pWriter</code>.
 */
public static OutputStream newEncoder(final Writer pWriter, int pLineSize, String pSeparator) {
    final Encoder encoder = new Encoder(new char[4096], pLineSize, pSeparator) {
        protected void writeBuffer(char[] pBuffer, int pOffset, int pLen) throws IOException {
            pWriter.write(pBuffer, pOffset, pLen);
        }
    };
    return new EncoderOutputStream(encoder);
}

From source file:IOUtil.java

/**
 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
 * @param bufferSize Size of internal buffer to use.
 *//*from  w  w w  .  ja  va2  s .co m*/
public static void copy(final Reader input, final Writer output, final int bufferSize) throws IOException {
    final char[] buffer = new char[bufferSize];
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.IO.java

/** Copy Reader to Writer for byteCount bytes or until EOF or exception.
 *///from w ww.  j a  v a2 s.  c  o  m
public static void copy(Reader in, Writer out, long byteCount) throws IOException {
    char buffer[] = new char[bufferSize];
    int len = bufferSize;

    if (byteCount >= 0) {
        while (byteCount > 0) {
            if (byteCount < bufferSize)
                len = in.read(buffer, 0, (int) byteCount);
            else
                len = in.read(buffer, 0, bufferSize);

            if (len == -1)
                break;

            byteCount -= len;
            out.write(buffer, 0, len);
        }
    } else {
        while (true) {
            len = in.read(buffer, 0, bufferSize);
            if (len == -1)
                break;
            out.write(buffer, 0, len);
        }
    }
}

From source file:Base64Utils.java

public static void base64Encode(byte[] data, int off, int len, Writer writer) throws IOException {
    if (len <= 0) {
        return;/*from   w  w  w.j a  v a  2s  . com*/
    }
    char[] out = new char[4];
    int rindex = off;
    int rest = len - off;
    int output = 0;
    while (rest >= 3) {
        int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
        out[0] = S_BASE64CHAR[i >> 18];
        out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
        out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
        out[3] = S_BASE64CHAR[i & 0x3f];
        writer.write(out, 0, 4);
        rindex += 3;
        rest -= 3;
        output += 4;
        if (output % 76 == 0) {
            writer.write("\n");
        }
    }
    if (rest == 1) {
        int i = data[rindex] & 0xff;
        out[0] = S_BASE64CHAR[i >> 2];
        out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
        out[2] = S_BASE64PAD;
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    } else if (rest == 2) {
        int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
        out[0] = S_BASE64CHAR[i >> 10];
        out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
        out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    }
}

From source file:Base64.java

/**
 * Outputs base64 representation of the specified data to a
 * <code>Writer</code>./*from w  w  w. j av a2 s. c o  m*/
 *
 * @param data   data to be encoded
 * @param off    offset within data at which to start encoding
 * @param len    length of data to encode
 * @param writer writer to output the encoded data
 * @throws java.io.IOException if an i/o error occurs
 */
public static void encode(byte[] data, int off, int len, Writer writer) throws IOException {
    if (len == 0) {
        return;
    }
    if (len < 0 || off >= data.length || len + off > data.length) {
        throw new IllegalArgumentException();
    }
    char[] enc = new char[4];
    while (len >= 3) {
        int i = ((data[off] & 0xff) << 16) + ((data[off + 1] & 0xff) << 8) + (data[off + 2] & 0xff);
        enc[0] = BASE64CHARS[i >> 18];
        enc[1] = BASE64CHARS[(i >> 12) & 0x3f];
        enc[2] = BASE64CHARS[(i >> 6) & 0x3f];
        enc[3] = BASE64CHARS[i & 0x3f];
        writer.write(enc, 0, 4);
        off += 3;
        len -= 3;
    }
    // add padding if necessary
    if (len == 1) {
        int i = data[off] & 0xff;
        enc[0] = BASE64CHARS[i >> 2];
        enc[1] = BASE64CHARS[(i << 4) & 0x3f];
        enc[2] = BASE64PAD;
        enc[3] = BASE64PAD;
        writer.write(enc, 0, 4);
    } else if (len == 2) {
        int i = ((data[off] & 0xff) << 8) + (data[off + 1] & 0xff);
        enc[0] = BASE64CHARS[i >> 10];
        enc[1] = BASE64CHARS[(i >> 4) & 0x3f];
        enc[2] = BASE64CHARS[(i << 2) & 0x3f];
        enc[3] = BASE64PAD;
        writer.write(enc, 0, 4);
    }
}

From source file:Base64.java

/**
 * Outputs base64 representation of the specified byte array to a character stream.
 * @param data The data to be encoded/*from  w  ww.ja  va2 s  .c om*/
 * @param off The offset within the data at which to start encoding
 * @param len The length of the data to encode
 * @param writer The Writer to which the encoded data should be
 *               written
 */
public static void encode(byte[] data, int off, int len, Writer writer) throws IOException {
    if (len <= 0) {
        return;
    }
    char[] out = new char[4];
    int rindex = off;
    int rest = len;
    int output = 0;
    while (rest >= 3) {
        int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
        out[0] = S_BASE64CHAR[i >> 18];
        out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
        out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
        out[3] = S_BASE64CHAR[i & 0x3f];
        writer.write(out, 0, 4);
        rindex += 3;
        rest -= 3;
        output += 4;
        if (output % 76 == 0) {
            writer.write("\n");
        }
    }
    if (rest == 1) {
        int i = data[rindex] & 0xff;
        out[0] = S_BASE64CHAR[i >> 2];
        out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
        out[2] = S_BASE64PAD;
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    } else if (rest == 2) {
        int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
        out[0] = S_BASE64CHAR[i >> 10];
        out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
        out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    }
}

From source file:it.unipmn.di.dcs.common.conversion.Base64.java

/**
 * Outputs base64 representation of the specified byte array to a character
 * stream.//from  w w  w  . j a va2s.c o  m
 *
 * Original method name is <em>encode</em>.
 */
public static void Encode(byte[] data, int off, int len, Writer writer) throws IOException {
    if (len <= 0)
        return;
    char[] out = new char[4];
    int rindex = off;
    int rest = len - off;
    int output = 0;
    while (rest >= 3) {
        int i = ((data[rindex] & 0xff) << 16) + ((data[rindex + 1] & 0xff) << 8) + (data[rindex + 2] & 0xff);
        out[0] = S_BASE64CHAR[i >> 18];
        out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
        out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
        out[3] = S_BASE64CHAR[i & 0x3f];
        writer.write(out, 0, 4);
        rindex += 3;
        rest -= 3;
        output += 4;
        if (output % 76 == 0)
            writer.write("\n");
    }
    if (rest == 1) {
        int i = data[rindex] & 0xff;
        out[0] = S_BASE64CHAR[i >> 2];
        out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
        out[2] = S_BASE64PAD;
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    } else if (rest == 2) {
        int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
        out[0] = S_BASE64CHAR[i >> 10];
        out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
        out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
        out[3] = S_BASE64PAD;
        writer.write(out, 0, 4);
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.meridio.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {//from   w w  w .j a va2 s  .  c  om
            String charSet = methodThread.getCharSet();
            if (charSet == null)
                charSet = "utf-8";
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}

From source file:org.apache.manifoldcf.connectorcommon.common.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {/* w ww .j  a va 2 s  .  co  m*/
            Charset charSet = methodThread.getCharSet();
            if (charSet == null)
                charSet = StandardCharsets.UTF_8;
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}

From source file:ChannelToWriter.java

/**
 * Read bytes from the specified channel, decode them using the specified
 * Charset, and write the resulting characters to the specified writer
 *///www  .  ja  v a  2  s  .c o  m
public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException {
    // Get and configure the CharsetDecoder we'll use
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    // Get the buffers we'll use, and the backing array for the CharBuffer.
    ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024);
    CharBuffer chars = CharBuffer.allocate(2 * 1024);
    char[] array = chars.array();

    while (channel.read(bytes) != -1) { // Read from channel until EOF
        bytes.flip(); // Switch to drain mode for decoding
        // Decode the byte buffer into the char buffer.
        // Pass false to indicate that we're not done.
        decoder.decode(bytes, chars, false);

        // Put the char buffer into drain mode, and write its contents
        // to the Writer, reading them from the backing array.
        chars.flip();
        writer.write(array, chars.position(), chars.remaining());

        // Discard all bytes we decoded, and put the byte buffer back into
        // fill mode. Since all characters were output, clear that buffer.
        bytes.compact(); // Discard decoded bytes
        chars.clear(); // Clear the character buffer
    }

    // At this point there may still be some bytes in the buffer to decode
    // So put the buffer into drain mode call decode() a final time, and
    // finish with a flush().
    bytes.flip();
    decoder.decode(bytes, chars, true); // True means final call
    decoder.flush(chars); // Flush any buffered chars
    // Write these final chars (if any) to the writer.
    chars.flip();
    writer.write(array, chars.position(), chars.remaining());
    writer.flush();
}