Example usage for java.io CharArrayWriter CharArrayWriter

List of usage examples for java.io CharArrayWriter CharArrayWriter

Introduction

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

Prototype

public CharArrayWriter(int initialSize) 

Source Link

Document

Creates a new CharArrayWriter with the specified initial size.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter(100);

    chw.write(str);//  w  w  w . j  a  v a2 s.  co  m

    System.out.println(chw.toString());

}

From source file:Main.java

/**
 * Fully reads the characters available from the supplied Reader
 * and returns these characters as a String object.
 *
 * @param reader The Reader to read the characters from.
 * @return A String existing of the characters that were read.
 * @throws IOException If I/O error occurred.
 *//*  ww  w  .j a  v  a 2  s .com*/
public static final String readFully(Reader reader) throws IOException {
    CharArrayWriter out = new CharArrayWriter(4096);
    transfer(reader, out);
    out.close();

    return out.toString();
}

From source file:ee.ria.xroad.common.util.PasswordStore.java

private static char[] byteToChar(byte[] bytes) throws IOException {
    if (bytes == null) {
        return null;
    }/*from  ww w.  j a  va 2  s . c o m*/

    CharArrayWriter writer = new CharArrayWriter(bytes.length);
    WriterOutputStream os = new WriterOutputStream(writer, UTF_8);
    os.write(bytes);
    os.close();

    return writer.toCharArray();
}

From source file:Main.java

/** Reads an <code>InputStream</code> into a string.
 * @param in The stream to read into a string.
 * @return The stream as a string//from  www.j a  va 2 s . c o m
 * @throws IOException
 */
public static String toString(InputStream in) throws IOException {
    if (in == null) {
        return null;
    }
    final Reader reader = new InputStreamReader(in);
    final char[] buffer = new char[100];
    final CharArrayWriter writer = new CharArrayWriter(1000);
    int read;
    while ((read = reader.read(buffer)) > -1)
        writer.write(buffer, 0, read);
    return writer.toString();
}

From source file:org.pentaho.di.trans.steps.ssh.SSHData.java

public static Connection OpenConnection(String serveur, int port, String username, String password,
        boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space,
        String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
    Connection conn = null;// w  w w . j  ava 2 s. com
    char[] content = null;
    boolean isAuthenticated = false;
    try {
        // perform some checks
        if (useKey) {
            if (Utils.isEmpty(keyFilename)) {
                throw new KettleException(
                        BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
            }
            FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);

            if (!keyFileObject.exists()) {
                throw new KettleException(
                        BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
            }

            FileContent keyFileContent = keyFileObject.getContent();

            CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());

            try (InputStream in = keyFileContent.getInputStream()) {
                IOUtils.copy(in, charArrayWriter);
            }

            content = charArrayWriter.toCharArray();
        }
        // Create a new connection
        conn = createConnection(serveur, port);

        /* We want to connect through a HTTP proxy */
        if (!Utils.isEmpty(proxyhost)) {
            /* Now connect */
            // if the proxy requires basic authentication:
            if (!Utils.isEmpty(proxyusername)) {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
            } else {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
            }
        }

        // and connect
        if (timeOut == 0) {
            conn.connect();
        } else {
            conn.connect(null, 0, timeOut * 1000);
        }
        // authenticate
        if (useKey) {
            isAuthenticated = conn.authenticateWithPublicKey(username, content,
                    space.environmentSubstitute(passPhrase));
        } else {
            isAuthenticated = conn.authenticateWithPassword(username, password);
        }
        if (isAuthenticated == false) {
            throw new KettleException(
                    BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
        }
    } catch (Exception e) {
        // Something wrong happened
        // do not forget to disconnect if connected
        if (conn != null) {
            conn.close();
        }
        throw new KettleException(
                BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
    }
    return conn;
}

From source file:org.batoo.jpa.jdbc.ValueConverter.java

private static Object readLob(Object value, Class<?> javaType) {
    try {//ww w.  j ava2s  .  c  o  m
        if (value instanceof Clob) {
            final Clob clob = (Clob) value;

            if (javaType == String.class) {
                final StringWriter w = new StringWriter();
                IOUtils.copy(clob.getAsciiStream(), w);
                value = w.toString();
            } else {
                final CharArrayWriter w = new CharArrayWriter((int) clob.length());
                IOUtils.copy(clob.getCharacterStream(), w);
                value = w.toCharArray();
            }
        } else if (value instanceof byte[]) {
            if (javaType == String.class) {
                final StringWriter w = new StringWriter();
                IOUtils.copy(new ByteArrayInputStream((byte[]) value), w);
                value = w.toString();
            } else if (javaType == char[].class) {
                final byte[] byteArray = (byte[]) value;

                final char[] charArray = new char[byteArray.length];

                for (int i = 0; i < charArray.length; i++) {
                    charArray[i] = (char) byteArray[i];
                }

                value = charArray;
            } else if (javaType != byte[].class) {
                final ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream((byte[]) value));
                try {
                    return is.readObject();
                } finally {
                    is.close();
                }
            }
        } else if (value instanceof String) {
            return value;
        } else {
            final Blob blob = (Blob) value;

            if (javaType == byte[].class) {
                final ByteArrayOutputStream os = new ByteArrayOutputStream();

                IOUtils.copy(blob.getBinaryStream(), os);

                value = os.toByteArray();
            } else {

                final ObjectInputStream is = new ObjectInputStream(blob.getBinaryStream());
                try {
                    value = is.readObject();
                } finally {
                    is.close();
                }
            }
        }
        return value;
    } catch (final Exception e) {
        throw new PersistenceException("Cannot read sql data", e);
    }
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyNullReader() throws IOException {
    Writer writer = new CharArrayWriter(11);
    assertEquals(0, StreamUtil.copy(null, writer));
    String resultStr = writer.toString();
    writer.close();//from w  ww  .  j a  va2 s . c om
    assertEquals("", writer.toString());
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTest.java

public void testToReader() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.calculateTotalCharsUnread());
    copy(input, charsOut, 2048);/*from  w ww. j ava  2 s.  c o  m*/
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));
    assertEquals(0, charBuffer.charsAvailable());
    assertEquals(0, charBuffer.size());
}

From source file:org.lockss.util.TestStreamUtil.java

public void testCopyReader() throws IOException {
    Reader reader = new StringReader("test string");
    Writer writer = new CharArrayWriter(11);
    assertEquals(11, StreamUtil.copy(reader, writer));
    reader.close();//  w w w.  j  a  v  a2s  .  c  om
    String resultStr = writer.toString();
    writer.close();
    assertTrue(resultStr.equals("test string"));
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTest.java

public void testToReaderOneByOne() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.calculateTotalCharsUnread());
    copyOneByOne(input, charsOut);/*from   ww  w  . j  a v  a 2s.  co m*/
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));
    assertEquals(0, charBuffer.charsAvailable());
    assertEquals(0, charBuffer.size());
}