List of usage examples for java.io ByteArrayOutputStream toString
public synchronized String toString()
From source file:com.f16gaming.pathofexilestatistics.RetrieveStatsTask.java
@Override protected StatsResponse doInBackground(String... urls) { try {// ww w . ja v a 2 s .c o m HttpGet get = new HttpGet(urls[0]); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(get); StatsResponse statsResponse = null; StatusLine status = response.getStatusLine(); if (status.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); String responseString = out.toString(); statsResponse = new StatsResponse(status, responseString); } else { response.getEntity().getContent().close(); } return statsResponse; } catch (Exception e) { return null; } }
From source file:org.bin01.db.verifier.JsonEventClient.java
@Override public <T> void postEvent(T event) throws IOException { try {//www .j a v a2s . c o m ByteArrayOutputStream buffer = new ByteArrayOutputStream(); JsonGenerator generator = factory.createGenerator(buffer, JsonEncoding.UTF8); serializer.serialize(event, generator); out.println(buffer.toString().trim()); } catch (IOException e) { throw Throwables.propagate(e); } }
From source file:fr.aliasource.webmail.server.LemonSSOProvider.java
@Override public Credentials obtainCredentials(Map<String, String> settings, HttpServletRequest req) { String ticket = req.getParameter("ticket"); if (ticket == null) { logger.warn("no ticket in url"); return null; }//w w w . j ava2 s . com try { URL url = new URL(settings.get(SSO_VALIDATE_URL) + "?action=validate&ticket=" + ticket); InputStream in = url.openStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); transfer(in, out, true); String content = out.toString(); if (logger.isDebugEnabled()) { logger.debug("SSO server returned:\n" + content); } Credentials creds = null; if (!content.equals("invalidOBMTicket")) { String[] ssoServerReturn = content.split("&password="); String login = ssoServerReturn[0].substring("login=".length()); String pass = ssoServerReturn[1]; creds = new Credentials(URLDecoder.decode(login, "UTF-8"), URLDecoder.decode(pass, "UTF-8")); } return creds; } catch (Exception e) { logger.error("Ticket validation error: " + e.getMessage(), e); return null; } }
From source file:org.zht.framework.web.converter.MappingFastjsonHttpMessageConverter.java
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i;//from w w w. j a v a2s . c om while ((i = inputMessage.getBody().read()) != -1) { baos.write(i); } return JSON.parseArray(baos.toString(), clazz); }
From source file:hudson.console.AnnotatedLargeTextTest.java
@Issue("SECURITY-382") @Test// w w w .j ava2 s . com public void oldDeserialization() throws Exception { ByteBuffer buf = new ByteBuffer(); buf.write(("hello" + ConsoleNote.PREAMBLE_STR + "AAAAwR+LCAAAAAAAAP9dzLEOwVAUxvHThtiNprYxsGiMQhiwNSIhMR/tSZXr3Lr3oJPwPt7FM5hM3gFh8i3/5Bt+1yeUrYH6ap9Yza1Ys9WKWuMiR05wqWhEgpmyEy306Jxvwb19ccGNoBJjLplmgWq0xgOGCjkNZ2IyTrsRlFayVTs4gVMYqP3pw28/JnznuABF/rYWyIyeJfLQe1vxZiDQ7NnYZLn0UZGRRjA9MiV+0OyFv3+utadQyH8B+aJxVM4AAAA=" + ConsoleNote.POSTAMBLE_STR + "there\n").getBytes()); AnnotatedLargeText<Void> text = new AnnotatedLargeText<>(buf, Charsets.UTF_8, true, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); text.writeLogTo(0, baos); assertEquals("hellothere\n", baos.toString()); StringWriter w = new StringWriter(); text.writeHtmlTo(0, w); assertEquals("hellothere\n", w.toString()); assertThat(logging.getMessages(), hasItem("Failed to resurrect annotation")); // TODO assert that this is IOException: Refusing to deserialize unsigned note from an old log. ConsoleNote.INSECURE = true; try { w = new StringWriter(); text.writeHtmlTo(0, w); assertThat(w.toString(), containsString("<script>")); } finally { ConsoleNote.INSECURE = false; } }
From source file:com.norconex.importer.handler.transformer.impl.ReduceConsecutivesTransformerTest.java
@Test public void testTransformTextDocument() throws ImporterHandlerException { String text = "\t\tThis is the text TeXt I want to modify...\n\r\n\r" + " Too much space."; ReduceConsecutivesTransformer t = new ReduceConsecutivesTransformer(); Reader reader = new InputStreamReader(IOUtils.toInputStream(xml)); try {/*from www .jav a 2s .c om*/ t.loadFromXML(reader); } catch (IOException e) { throw new ImporterHandlerException("Could not reduce consecutives.", e); } finally { IOUtils.closeQuietly(reader); } InputStream is = IOUtils.toInputStream(text); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { t.transformDocument("dummyRef", is, os, new ImporterMetadata(), true); String response = os.toString(); System.out.println(response); Assert.assertEquals("\tthis is the text i want to modify.\n\r too much space.", response.toLowerCase()); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
From source file:com.scvngr.levelup.core.net.request.factory.TicketRequestFactoryTest.java
/** * Tests building a support request./*from w w w. j a v a2 s. c o m*/ * * @throws com.scvngr.levelup.core.net.AbstractRequest.BadRequestException on test failure. * @throws java.io.IOException on test failure. */ @SmallTest public void testBuildSupportRequest() throws BadRequestException, IOException { final TicketRequestFactory builder = new TicketRequestFactory(getContext(), new MockAccessTokenRetriever()); final AbstractRequest request = builder.buildSupportRequest(MESSAGE_FIXTURE); assertNotNull(request); final URL url = request.getUrl(getContext()); assertTrue("hits /tickets endpoint", url.getPath().endsWith("/tickets")); assertTrue(request.getRequestHeaders(getContext()).containsKey(LevelUpRequest.HEADER_AUTHORIZATION)); assertEquals(RequestUtils.HEADER_CONTENT_TYPE_JSON, request.getRequestHeaders(getContext()).get(HTTP.CONTENT_TYPE)); assertEquals(HttpMethod.POST, request.getMethod()); assertTrue(url.getPath().startsWith("/v14")); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { request.writeBodyToStream(getContext(), baos); assertTrue("Has expected content", baos.toString().contains(MESSAGE_FIXTURE)); } finally { baos.close(); } }
From source file:hudson.console.AnnotatedLargeTextTest.java
@Issue("SECURITY-382") @Test/* w ww . j ava2s. com*/ public void badMac() throws Exception { ByteBuffer buf = new ByteBuffer(); buf.write(("Go back to " + ConsoleNote.PREAMBLE_STR + "////4ByIhqPpAc43AbrEtyDUDc1/UEOXsoY6LeoHSeSlb1d7AAAAlR+LCAAAAAAAAP9b85aBtbiIQS+jNKU4P08vOT+vOD8nVc8xLy+/JLEkNcUnsSg9NSS1oiQktbhEBUT45ZekCpys9xWo8J3KxMDkycCWk5qXXpLhw8BcWpRTwiDkk5VYlqifk5iXrh9cUpSZl25dUcQghWaBM4QGGcYAAYxMDAwVBUAGZwkDq35Rfn4JABmN28qcAAAA" + ConsoleNote.POSTAMBLE_STR + "your home.\n").getBytes()); AnnotatedLargeText<Void> text = new AnnotatedLargeText<>(buf, Charsets.UTF_8, true, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); text.writeLogTo(0, baos); assertEquals("Go back to your home.\n", baos.toString()); StringWriter w = new StringWriter(); text.writeHtmlTo(0, w); assertEquals("Go back to your home.\n", w.toString()); assertThat(logging.getMessages(), hasItem("Failed to resurrect annotation")); // TODO assert that this is IOException: MAC mismatch }
From source file:net.sf.dynamicreports.googlecharts.test.AbstractJasperTest.java
protected void build() throws DRException { jasperReport = reportBuilder.toJasperReport(); jasperPrint = reportBuilder.toJasperPrint(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); reportBuilder.toHtml(bos);//from w w w .j a v a2 s.co m html = bos.toString(); }
From source file:com.cedarsoft.serialization.test.performance.JacksonTest.java
@Test public void testMapper() throws Exception { ObjectMapper mapper = new ObjectMapper(); com.cedarsoft.serialization.test.performance.jaxb.FileType fileType = new com.cedarsoft.serialization.test.performance.jaxb.FileType( "Canon Raw", new com.cedarsoft.serialization.test.performance.jaxb.Extension(".", "cr2", true), false);//from w w w. j av a 2 s . c o m ByteArrayOutputStream out = new ByteArrayOutputStream(); mapper.writeValue(out, fileType); JsonUtils.assertJsonEquals(JSON, out.toString()); }