Example usage for java.io CharArrayWriter toCharArray

List of usage examples for java.io CharArrayWriter toCharArray

Introduction

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

Prototype

public char[] toCharArray() 

Source Link

Document

Returns a copy of the input data.

Usage

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;/*ww  w  . ja  va2s.  c  om*/
    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:Base64.java

private static char[] readChars(File file) {
    CharArrayWriter caw = new CharArrayWriter();
    try {/* w  ww .j a  va  2s. c om*/
        Reader fr = new FileReader(file);
        Reader in = new BufferedReader(fr);
        int count = 0;
        char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0)
                caw.write(buf, 0, count);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:Base64.java

private static char[] readChars(final File file) {
    final CharArrayWriter caw = new CharArrayWriter();
    try {//w ww .  ja va2s . c  o  m
        final Reader fr = new FileReader(file);
        final Reader in = new BufferedReader(fr);
        int count;
        final char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0) {
                caw.write(buf, 0, count);
            }
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:org.roda.core.common.RodaUtils.java

public static Reader applyMetadataStylesheet(Binary binary, String basePath, String metadataType,
        String metadataVersion, Map<String, String> parameters) throws GenericException {
    try (Reader descMetadataReader = new InputStreamReader(
            new BOMInputStream(binary.getContent().createInputStream()))) {

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource source = new InputSource(descMetadataReader);
        Source text = new SAXSource(xmlReader, source);

        XsltExecutable xsltExecutable = CACHE.get(Triple.of(basePath, metadataType, metadataVersion));

        XsltTransformer transformer = xsltExecutable.load();
        CharArrayWriter transformerResult = new CharArrayWriter();

        transformer.setSource(text);//from  www  .  j a v a  2 s.  c  o  m
        transformer.setDestination(PROCESSOR.newSerializer(transformerResult));

        for (Entry<String, String> parameter : parameters.entrySet()) {
            QName qName = new QName(parameter.getKey());
            XdmValue xdmValue = new XdmAtomicValue(parameter.getValue());
            transformer.setParameter(qName, xdmValue);
        }

        transformer.transform();

        return new CharArrayReader(transformerResult.toCharArray());

    } catch (IOException | SAXException | ExecutionException | SaxonApiException e) {
        throw new GenericException("Could not process descriptive metadata binary " + binary.getStoragePath()
                + " metadata type " + metadataType + " and version " + metadataVersion, e);
    }
}

From source file:org.roda.core.common.RodaUtils.java

public static Reader applyEventStylesheet(Binary binary, boolean onlyDetails, Map<String, String> translations,
        String path) throws GenericException {
    try (Reader descMetadataReader = new InputStreamReader(
            new BOMInputStream(binary.getContent().createInputStream()))) {

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource source = new InputSource(descMetadataReader);
        Source text = new SAXSource(xmlReader, source);

        XsltExecutable xsltExecutable = EVENT_CACHE.get(path);

        XsltTransformer transformer = xsltExecutable.load();
        CharArrayWriter transformerResult = new CharArrayWriter();

        transformer.setSource(text);//from www  . j a v  a 2 s.  c om
        transformer.setDestination(PROCESSOR.newSerializer(transformerResult));

        // send param to filter stylesheet work
        transformer.setParameter(new QName("onlyDetails"), new XdmAtomicValue(Boolean.toString(onlyDetails)));

        for (Entry<String, String> parameter : translations.entrySet()) {
            QName qName = new QName(parameter.getKey());
            XdmValue xdmValue = new XdmAtomicValue(parameter.getValue());
            transformer.setParameter(qName, xdmValue);
        }

        transformer.transform();
        return new CharArrayReader(transformerResult.toCharArray());
    } catch (IOException | SAXException | ExecutionException | SaxonApiException e) {
        LOGGER.error(e.getMessage(), e);
        throw new GenericException("Could not process event binary " + binary.getStoragePath(), e);
    }
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Get the contents of a <code>Reader</code> as a character array.
 * <p>/*from w  ww . j  ava2  s  .com*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * 
 * @param input the <code>Reader</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(final Reader input) throws IOException {
    final CharArrayWriter sw = new CharArrayWriter();
    copy(input, sw);
    return sw.toCharArray();
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the default character encoding of the platform.
 * <p>/*from   w  w  w  .  j a  v  a2  s. c om*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is the <code>InputStream</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(final InputStream is) throws IOException {
    final CharArrayWriter output = new CharArrayWriter();
    copy(is, output);
    return output.toCharArray();
}

From source file:IOUtils.java

/**
 * Get the contents of a <code>Reader</code> as a character array.
 * <p>//ww  w .j a va  2  s.c om
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * 
 * @param input  the <code>Reader</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(Reader input) throws IOException {
    CharArrayWriter sw = new CharArrayWriter();
    copy(input, sw);
    return sw.toCharArray();
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the specified character encoding.
 * <p>/*from w w w  .  ja v a2 s. com*/
 * Character encoding names can be found at <a
 * href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is the <code>InputStream</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(final InputStream is, final String encoding) throws IOException {
    final CharArrayWriter output = new CharArrayWriter();
    copy(is, output, encoding);
    return output.toCharArray();
}

From source file:IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the default character encoding of the platform.
 * <p>/* w  w w .  j a v a 2  s.  co  m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is  the <code>InputStream</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(InputStream is) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    copy(is, output);
    return output.toCharArray();
}