List of usage examples for java.io ByteArrayOutputStream toString
@Deprecated public synchronized String toString(int hibyte)
From source file:Main.java
public static String Document2String(Document doc) throws IOException, TransformerException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(baos, "UTF-8"))); return baos.toString("UTF-8"); }
From source file:org.lightcouch.CouchDbClientBase.java
/** * Performs a HTTP PUT request, saves or updates a document. * @return {@link Response}//ww w . j a v a 2s. c o m */ public static String modelToString(Object object) { // throw new UnsupportedOperationException(); String string = null; Map options = new HashMap<Object, Object>(); options.put(EMFJs.OPTION_INDENT_OUTPUT, false); JSONSave js = new JSONSave(options); if (object instanceof EObject) { EObject eo = (EObject) object; org.codehaus.jackson.JsonNode node = js.writeEObject(eo, eo.eResource()); string = node.toString(); } else { ByteArrayOutputStream os = new ByteArrayOutputStream(); js.writeValue(os, object); try { string = os.toString(BTSConstants.ENCODING); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return string; }
From source file:de.zib.scalaris.examples.wikipedia.data.Revision.java
/** * De-compresses the given text and returns it as a string. * /* w w w. j av a 2s . com*/ * @param text * the compressed text * * @return the de-compressed text * * @throws RuntimeException * if de-compressing the text did not work */ protected static String unpackText(byte[] text) throws RuntimeException { try { ByteArrayOutputStream unpacked = new ByteArrayOutputStream(); ByteArrayInputStream bis = new ByteArrayInputStream(text); GZIPInputStream gis = new GZIPInputStream(bis); byte[] bbuf = new byte[256]; int read = 0; while ((read = gis.read(bbuf)) >= 0) { unpacked.write(bbuf, 0, read); } gis.close(); return new String(unpacked.toString("UTF-8")); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:carisma.ui.eclipse.CarismaGUI.java
static String getString(InputStream is) { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length;// w ww . j ava 2s. co m try { while ((length = is.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:eu.europa.esig.dss.asic.validation.ASiCContainerValidator.java
private static MimeType getMimeType(final DSSDocument mimeType) throws DSSException { try {/*from w w w. java 2 s .c om*/ final InputStream inputStream = mimeType.openStream(); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(inputStream, byteArrayOutputStream); final String mimeTypeString = byteArrayOutputStream.toString("UTF-8"); final MimeType asicMimeType = MimeType.fromMimeTypeString(mimeTypeString); return asicMimeType; } catch (IOException e) { throw new DSSException(e); } }
From source file:groovesquid.Grooveshark.java
public static String sendRequest(String method, HashMap<String, Object> parameters) { if (tokenExpires <= new Date().getTime() && tokenExpires != 0 && !"initiateSession".equals(method) && !"getCommunicationToken".equals(method) && !"getCountry".equals(method)) { try {//w w w.ja v a 2s. c o m InitThread initThread = new InitThread(); initThread.start(); initThread.getLatch().await(); } catch (InterruptedException ex) { log.log(Level.SEVERE, null, ex); } } String responseContent = null; HttpEntity httpEntity = null; try { Client client = clients.getHtmlshark(); String protocol = "http://"; if (method.equals("getCommunicationToken")) { protocol = "https://"; } String url = protocol + "grooveshark.com/more.php?" + method; for (String jsqueueMethod : jsqueueMethods) { if (jsqueueMethod.equals(method)) { client = clients.getJsqueue(); break; } } header.put("client", client.getName()); header.put("clientRevision", client.getRevision()); header.put("privacy", "0"); header.put("uuid", uuid); header.put("country", country); if (!method.equals("initiateSession")) { header.put("session", session); header.put("token", generateToken(method, client.getSecret())); } Gson gson = new Gson(); String jsonString = gson.toJson(new JsonRequest(header, parameters, method)); log.info(">>> " + jsonString); HttpPost httpPost = new HttpPost(url); httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json"); httpPost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); httpPost.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"); httpPost.setHeader("Referer", "http://grooveshark.com/JSQueue.swf?" + client.getRevision()); httpPost.setHeader("Content-Language", "en-US"); httpPost.setHeader("Cache-Control", "max-age=0"); httpPost.setHeader("Accept", "*/*"); httpPost.setHeader("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.3"); httpPost.setHeader("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); httpPost.setHeader("Origin", "http://grooveshark.com"); if (!method.equals("initiateSession")) { httpPost.setHeader("Cookie", "PHPSESSID=" + session); } httpPost.setEntity(new StringEntity(jsonString, "UTF-8")); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (Main.getConfig().getProxyHost() != null && Main.getConfig().getProxyPort() != null) { httpClientBuilder .setProxy(new HttpHost(Main.getConfig().getProxyHost(), Main.getConfig().getProxyPort())); } HttpClient httpClient = httpClientBuilder.build(); HttpResponse httpResponse = httpClient.execute(httpPost); 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("method " + method + ": " + statusLine); } responseContent = baos.toString("UTF-8"); } catch (Exception ex) { Logger.getLogger(Grooveshark.class.getName()).log(Level.SEVERE, null, ex); } finally { try { EntityUtils.consume(httpEntity); } catch (IOException ex) { Logger.getLogger(Grooveshark.class.getName()).log(Level.SEVERE, null, ex); } } log.info("<<< " + responseContent); return responseContent; }
From source file:ee.ria.xroad.common.message.SoapUtils.java
/** * Returns the XML representing the SOAP message. * @param soap message to be converted into XML * @param charset charset to use when generating the XML string * @return XML String representing the soap message * @throws IOException in case of errors *///from ww w. jav a2 s .c o m public static String getXml(SOAPMessage soap, String charset) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { soap.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true"); soap.writeTo(out); } catch (SOAPException e) { // Avoid throwing SOAPException, since it is essentially an // IOException if we cannot produce the XML throw new IOException(e); } return out.toString(charset); }
From source file:com.meetingninja.csse.database.GroupDatabaseAdapter.java
public static Group updateGroup(Group group) throws IOException { ByteArrayOutputStream json = new ByteArrayOutputStream(); // this type of print stream allows us to get a string easily PrintStream ps = new PrintStream(json); JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8); // Build JSON Object for Title jgen.writeStartObject();//from w w w . j a v a 2 s. c o m jgen.writeStringField(Keys.Group.ID, group.getGroupID()); jgen.writeStringField("field", Keys.Group.TITLE); jgen.writeStringField("value", group.getGroupTitle()); jgen.writeEndObject(); jgen.close(); String payloadTitle = json.toString("UTF8"); ps.close(); json = new ByteArrayOutputStream(); // this type of print stream allows us to get a string easily ps = new PrintStream(json); jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8); // Build JSON Object for Group members jgen.writeStartObject(); jgen.writeStringField(Keys.Group.ID, group.getGroupID()); jgen.writeStringField("field", Keys.Group.MEMBERS); jgen.writeArrayFieldStart("value"); for (User member : group.getMembers()) { jgen.writeStartObject(); jgen.writeStringField(Keys.User.ID, member.getID()); jgen.writeEndObject(); } jgen.writeEndArray(); jgen.writeEndObject(); jgen.close(); String payloadMembers = json.toString("UTF8"); ps.close(); // Establish connection sendSingleEdit(payloadTitle); String response = sendSingleEdit(payloadMembers); JsonNode groupNode = MAPPER.readTree(response); return parseGroup(groupNode, new Group()); }
From source file:com.meetingninja.csse.database.NotesDatabaseAdapter.java
private static String getEditPayload(String noteID, 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();// w w w. j a v a 2 s . com jgen.writeStringField(Keys.Note.ID, noteID); jgen.writeStringField("field", field); jgen.writeStringField("value", value); jgen.writeEndObject(); jgen.close(); String payload = json.toString("UTF8"); ps.close(); Log.d("updatenotepayload", payload); return payload; }
From source file:Main.java
public static String getContent(Node node, boolean omitXMLDeclaration) { try {//ww w . j av a 2 s . co m ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty("encoding", "UTF-8"); if (omitXMLDeclaration) { transformer.setOutputProperty("omit-xml-declaration", "yes"); } DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(baos); transformer.transform(source, result); String cont = baos.toString("UTF8"); baos.close(); return cont; } catch (Exception ex) { return ""; } }