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.granule.json.utils.XML.java

/**
 * Method to take an input stream to an XML document and return a String of the JSON format.  Note that the xmlStream is not closed when read is complete.  This is left up to the caller, who may wish to do more with it.
 * @param xmlStream The InputStream to an XML document to transform to JSON.
 * @param verbose Boolean flag denoting whther or not to write the JSON in verbose (formatted), or compact form (no whitespace)
 * @return A string of the JSON representation of the XML file
 * /*  ww w .  ja v a2  s.c om*/
 * @throws SAXException Thrown if an error occurs during parse.
 * @throws IOException Thrown if an IOError occurs.
 */
public static String toJson(InputStream xmlStream, boolean verbose) throws SAXException, IOException {
    if (logger.isLoggable(Level.FINER)) {
        logger.exiting(className, "toJson(InputStream, boolean)");
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String result = null;

    try {
        toJson(xmlStream, baos, verbose);
        result = baos.toString("UTF-8");
        baos.close();
    } catch (UnsupportedEncodingException uec) {
        IOException iox = new IOException(uec.toString());
        iox.initCause(uec);
        throw iox;
    }

    if (logger.isLoggable(Level.FINER)) {
        logger.exiting(className, "toJson(InputStream, boolean)");
    }

    return result;
}

From source file:com.granule.json.utils.XML.java

/**
 * Method to take an input stream to an JSON document and return a String of the XML format.  Note that the JSONStream is not closed when read is complete.  This is left up to the caller, who may wish to do more with it.
 * @param xmlStream The InputStream to an JSON document to transform to XML.
 * @param verbose Boolean flag denoting whther or not to write the XML in verbose (formatted), or compact form (no whitespace)
 * @return A string of the JSON representation of the XML file
 * /*from  ww  w .j a va  2 s  .  c  o  m*/
 * @throws IOException Thrown if an IOError occurs.
 */
public static String toXml(InputStream JSONStream, boolean verbose) throws IOException {
    if (logger.isLoggable(Level.FINER)) {
        logger.exiting(className, "toXml(InputStream, boolean)");
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    String result = null;

    try {
        toXml(JSONStream, baos, verbose);
        result = baos.toString("UTF-8");
        baos.close();
    } catch (UnsupportedEncodingException uec) {
        IOException iox = new IOException(uec.toString());
        iox.initCause(uec);
        throw iox;
    }

    if (logger.isLoggable(Level.FINER)) {
        logger.exiting(className, "toXml(InputStream, boolean)");
    }

    return result;
}

From source file:crow.util.Util.java

/**
 * inputStream UTF-8??// ww w. j  av  a  2s.  c  om
 * 
 */
public static String inputStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte arr[] = new byte[32];
    int len = 0;
    try {
        while ((len = is.read(arr)) != -1) {
            out.write(arr, 0, len);
        }
    } finally {
        try {
            is.close();
            out.close();
        } catch (IOException e) {
            // do nothing
        }
    }
    return out.toString(HTTP.UTF_8);
}

From source file:com.meetingninja.csse.database.MeetingDatabaseAdapter.java

private static String getEditPayload(String meetingID, String field, String value) throws IOException {
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Title
    jgen.writeStartObject();//from  w w  w  .  j  a  va2 s  .co m
    jgen.writeStringField(Keys.Meeting.ID, meetingID);
    jgen.writeStringField("field", field);
    jgen.writeStringField("value", value);
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();
    return payload;
}

From source file:com.meetingninja.csse.database.TaskDatabaseAdapter.java

private static String getEditPayload(String taskID, String field, String value) throws IOException {
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Title
    jgen.writeStartObject();//from w w  w .j ava 2s .com
    jgen.writeStringField(Keys.Task.ID, taskID);
    jgen.writeStringField("field", field);
    jgen.writeStringField("value", value);
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();
    return payload;
}

From source file:com.aliyun.openservices.odps.console.utils.CommandParserUtils.java

private static String getCommandUsageString(Class<?> commandClass) {
    try {/*from   w  ww. ja  va2 s  .co  m*/
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(os);
        Method printMethod = commandClass.getDeclaredMethod("printUsage", new Class<?>[] { PrintStream.class });
        printMethod.invoke(null, ps);
        return os.toString("UTF8");

    } catch (Exception e) {
        return null;
    }
}

From source file:com.cloudera.api.model.ApiModelTest.java

static String objectToXml(Object object) throws JAXBException, UnsupportedEncodingException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    m.marshal(object, baos);//from   www .ja  v  a2  s . com
    return baos.toString(TEXT_ENCODING);
}

From source file:io.joynr.util.JoynrUtil.java

private static Object getResource(InputStream inputStream, boolean asByteArray) throws JoynrRuntimeException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
    byte[] bytes = new byte[512];

    // Read bytes from the input stream in bytes.length-sized chunks and
    // write/*  www  .ja v  a  2s.c  om*/
    // them into the output stream
    int readBytes;
    try {
        while ((readBytes = inputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, readBytes);
        }
        Object result = null;

        if (asByteArray) {
            result = ArrayUtils.toObject(outputStream.toByteArray());
        } else {
            result = outputStream.toString("UTF-8");

        }
        // Close the streams
        inputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        throw new JoynrRuntimeException(e.getMessage(), e) {
            private static final long serialVersionUID = 1L;
        };
    }
}

From source file:com.meetingninja.csse.database.ContactDatabaseAdapter.java

public static List<Contact> addContact(String contactUserID) throws IOException {

    String _url = getBaseUri().build().toString();
    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod(IRequest.PUT);
    addRequestHeader(conn, false);/*from  w ww .j av a  2 s  .c  o  m*/
    SessionManager session = SessionManager.getInstance();
    String userID = session.getUserID();
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    // this type of print stream allows us to get a string easily
    PrintStream ps = new PrintStream(json);
    // Create a generator to build the JSON string
    JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8);
    // Build JSON Object for Title
    jgen.writeStartObject();
    jgen.writeStringField(Keys.User.ID, userID);
    jgen.writeArrayFieldStart(Keys.User.CONTACTS);
    jgen.writeStartObject();
    jgen.writeStringField(Keys.User.CONTACTID, contactUserID);
    jgen.writeEndObject();
    jgen.writeEndArray();
    jgen.writeEndObject();
    jgen.close();
    String payload = json.toString("UTF8");
    ps.close();

    sendPostPayload(conn, payload);
    String response = getServerResponse(conn);

    // TODO: put add useful check here
    // User userContact=null;
    // String relationID=null;
    // String result = new String();
    // if (!response.isEmpty()) {
    // JsonNode contactNode = MAPPER.readTree(response);
    // if (!contactNode.has(Keys.User.ID)) {
    // result = "invalid";
    // } else {
    // result = contactNode.get(Keys.User.ID).asText();
    // userContact = getUserInfo(result);
    // relationID = contactNode.get(Keys.User.RELATIONID).asText();
    // }
    // }

    // if (!result.equalsIgnoreCase("invalid"))
    // g.setID(result);
    conn.disconnect();

    // Contact contact = new Contact(userContact,relationID);
    List<Contact> contacts = new ArrayList<Contact>();
    contacts = getContacts(userID);
    return contacts;
}

From source file:org.frameworkset.spi.remote.http.Client.java

public static String getFileContent(InputStream reader, String charSet) throws IOException {
    ByteArrayOutputStream swriter = null;
    OutputStream temp = null;/*from   w  ww.  ja v  a2 s  .c o  m*/

    try {
        //           reader = new FileInputStream(file);
        swriter = new ByteArrayOutputStream();
        temp = new BufferedOutputStream(swriter);

        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = reader.read(buffer)) > 0) {
            temp.write(buffer, 0, len);
        }
        temp.flush();
        if (charSet != null && !charSet.equals(""))
            return swriter.toString(charSet);
        else
            return swriter.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (IOException e) {
            }
        if (swriter != null)
            try {
                swriter.close();
            } catch (IOException e) {
            }
        if (temp != null)
            try {
                temp.close();
            } catch (IOException e) {
            }
    }
}