Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

@Deprecated
public synchronized String toString(int hibyte) 

Source Link

Document

Creates a newly allocated string.

Usage

From source file:com.zxy.commons.exec.CmdExecutor.java

/**
 * //from   ww  w .ja  va  2 s. c  o m
 * 
 * @param workHome workHome
 * @param command command
 * @return ???
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
public static ExecutorResult exec(String workHome, String command) throws InterruptedException, IOException {
    CommandLine cmdLine = CommandLine.parse(PRE_CMD + command);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(workHome));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
    executor.setStreamHandler(streamHandler);

    int code = executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment());
    String successMsg = outputStream.toString(ENCODING);
    String errorMsg = errorStream.toString(ENCODING);
    return new ExecutorResult(code, successMsg, errorMsg);
}

From source file:ilarkesto.net.ApacheHttpDownloader.java

private static String getText(HttpEntity entity, String charsetName)
        throws IOException, UnsupportedEncodingException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    entity.writeTo(buffer);//from   w ww  .  jav a2s .co  m
    return buffer.toString(charsetName == null ? IO.UTF_8 : charsetName);
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Method to convert Element to String//ww  w. j  av a2 s  . com
 *
 * @param e
 * @return
 */
public static String getStringFromElement(Element e) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.normalize();
        prettyPrint(e, baos);
        return baos.toString("UTF-8");
    } catch (Exception ex) {
        return null;
    }
}

From source file:Base64Decoder.java

/**
 * Returns the decoded form of the given encoded string.
 * //  w  w w  .  j a  v  a 2 s.c  om
 * @param encoded
 *            the string to decode
 * @return the decoded form of the encoded string
 */
public static String decode(String encoded) {
    byte[] bytes = null;
    try {
        bytes = encoded.getBytes("8859_1");
    } catch (UnsupportedEncodingException ignored) {
    }

    Base64Decoder in = new Base64Decoder(new ByteArrayInputStream(bytes));

    ByteArrayOutputStream out = new ByteArrayOutputStream((int) (bytes.length * 0.67));

    try {
        byte[] buf = new byte[4 * 1024]; // 4K buffer
        int bytesRead;
        while ((bytesRead = in.read(buf)) != -1) {
            out.write(buf, 0, bytesRead);
        }
        out.close();

        return out.toString("8859_1");
    } catch (IOException ignored) {
        return null;
    }
}

From source file:id.nci.stm_9.HkpKeyServer.java

static private String readAll(InputStream in, String encoding) throws IOException {
    ByteArrayOutputStream raw = new ByteArrayOutputStream();

    byte buffer[] = new byte[1 << 16];
    int n = 0;// ww w .j av  a  2 s .  c o  m
    while ((n = in.read(buffer)) != -1) {
        raw.write(buffer, 0, n);
    }

    if (encoding == null) {
        encoding = "utf8";
    }
    return raw.toString(encoding);
}

From source file:de.micromata.genome.gwiki.plugin.vfolder_1_0.GWikiVFolderUtils.java

public static String getHtmlSource(GWikiElement vfolderEl, GWikiVFolderNode fvn, String filePageId) {
    String localName = getLocalFromFilePageId(vfolderEl, filePageId);
    try {//from  w  ww  . j av  a2 s  . c om

        FsObject file = fvn.getFileSystem().getFileObject(localName);
        if (file.exists() == false) {
            return "NOT FOUND";
        }
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        fvn.getFileSystem().readBinaryFile(localName, bout);

        String s = bout.toString(fvn.getHtmlContentEncoding());
        return s;
    } catch (Exception ex) {
        GWikiLog.error("Error reading vfile: " + localName + "; " + ex.getMessage(), ex);
        return "Page cannot be read";
    }

}

From source file:groovesquid.GetAdsThread.java

public static String getFile(String url) {
    String responseContent = null;
    HttpEntity httpEntity = null;/*w w w  . j a  va 2  s. c  om*/
    try {
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
        httpGet.setHeader(HTTP.USER_AGENT,
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse httpResponse = httpClient.execute(httpGet);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        httpEntity = httpResponse.getEntity();

        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            httpEntity.writeTo(baos);
        } else {
            throw new RuntimeException(url);
        }

        responseContent = baos.toString("UTF-8");
    } catch (Exception ex) {
        log.log(Level.SEVERE, null, ex);
    } finally {
        try {
            EntityUtils.consume(httpEntity);
        } catch (IOException ex) {
            log.log(Level.SEVERE, null, ex);
        }
    }
    return responseContent;
}

From source file:com.geewhiz.pacify.test.TestUtil.java

private static void archiveContainsEntries(File replacedArchive, File expectedArchive)
        throws ArchiveException, IOException {
    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    FileInputStream expectedIS = new FileInputStream(expectedArchive);
    ArchiveInputStream expectedAIS = factory.createArchiveInputStream(new BufferedInputStream(expectedIS));
    ArchiveEntry expectedEntry = null;/*from  w  w w .  j av  a 2  s  .c  o m*/
    while ((expectedEntry = expectedAIS.getNextEntry()) != null) {
        FileInputStream replacedIS = new FileInputStream(replacedArchive);
        ArchiveInputStream replacedAIS = factory.createArchiveInputStream(new BufferedInputStream(replacedIS));

        ArchiveEntry replacedEntry = null;
        boolean entryFound = false;
        while ((replacedEntry = replacedAIS.getNextEntry()) != null) {
            Assert.assertNotNull("We expect an entry.", replacedEntry);
            if (!expectedEntry.getName().equals(replacedEntry.getName())) {
                continue;
            }
            entryFound = true;
            if (expectedEntry.isDirectory()) {
                Assert.assertTrue("we expect a directory", replacedEntry.isDirectory());
                break;
            }

            if (ArchiveUtils.isArchiveAndIsSupported(expectedEntry.getName())) {
                Assert.assertTrue("we expect a archive",
                        ArchiveUtils.isArchiveAndIsSupported(replacedEntry.getName()));

                File replacedChildArchive = ArchiveUtils.extractFile(replacedArchive,
                        ArchiveUtils.getArchiveType(replacedArchive), replacedEntry.getName());
                File expectedChildArchive = ArchiveUtils.extractFile(expectedArchive,
                        ArchiveUtils.getArchiveType(expectedArchive), expectedEntry.getName());

                archiveContainsEntries(replacedChildArchive, expectedChildArchive);

                replacedChildArchive.delete();
                expectedChildArchive.delete();

                break;
            }

            ByteArrayOutputStream expectedContent = readContent(expectedAIS);
            ByteArrayOutputStream replacedContent = readContent(replacedAIS);

            Assert.assertEquals("Content should be same of entry " + expectedEntry.getName(),
                    expectedContent.toString("UTF-8"), replacedContent.toString("UTF-8"));
            break;
        }

        replacedIS.close();
        Assert.assertTrue("Entry [" + expectedEntry.getName() + "] in the result archive expected.",
                entryFound);
    }

    expectedIS.close();
}

From source file:Base64Encoder.java

/**
 * Returns the encoded form of the given unencoded string.
 * //from www. j  a  va 2s .c o  m
 * @param unencoded
 *            the string to encode
 * @return the encoded form of the unencoded string
 */
public static String encode(String unencoded) {
    ByteArrayOutputStream out = new ByteArrayOutputStream((int) (unencoded.length() * 1.37));
    Base64Encoder encodedOut = new Base64Encoder(out);

    byte[] bytes = null;
    try {
        bytes = unencoded.getBytes("8859_1");
    } catch (UnsupportedEncodingException ignored) {
    }

    try {
        encodedOut.write(bytes);
        encodedOut.close();

        return out.toString("8859_1");
    } catch (IOException ignored) {
        return null;
    }
}

From source file:com.clustercontrol.plugin.impl.AsyncTask.java

/**
 * SerializableXML???//from   w  w  w.  j  a v a2  s  .  c om
 * @param obj Serializable
 * @return ??XML
 * @throws IOException
 */
public static String encodeXML(Serializable obj) throws IOException {
    ByteArrayOutputStream baos = null;
    XMLEncoder enc = null;
    String xml = null;

    try {
        baos = new ByteArrayOutputStream();
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        xml = baos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        log.warn(e);
    } finally {
        if (enc != null) {
            enc.close();
        }
        if (baos != null) {
            baos.close();
        }
    }

    return xml;
}