List of usage examples for java.io ByteArrayOutputStream reset
public synchronized void reset()
From source file:org.jboss.as.test.integration.management.cli.CliVariablesTestCase.java
/** * Tests if 'set' with no parameters prints all currently set variables * @throws Exception/*from w ww . j a v a 2s .c o m*/ */ @Test public void testSetPrintsAllVariables() throws Exception { final String VAR1_NAME = "variable_1"; final String VAR2_NAME = "variable_2"; final String VAR1_VALUE = "value_1"; final String VAR2_VALUE = "value_2"; final ByteArrayOutputStream cliOut = new ByteArrayOutputStream(); final CommandContext ctx = CLITestUtil.getCommandContext(cliOut); ctx.handle("set " + VAR1_NAME + "=" + VAR1_VALUE); ctx.handle("set " + VAR2_NAME + "=" + VAR2_VALUE); cliOut.reset(); ctx.handle("echo $" + VAR1_NAME); ctx.handle("echo $" + VAR2_NAME); String echoResult = cliOut.toString(); assertTrue(echoResult.contains(VAR1_VALUE)); assertTrue(echoResult.contains(VAR2_VALUE)); cliOut.reset(); ctx.handle("set"); //print all variables String setResult = cliOut.toString(); assertTrue(setResult.contains(VAR1_NAME + "=" + VAR1_VALUE)); assertTrue(setResult.contains(VAR2_NAME + "=" + VAR2_VALUE)); assertTrue(ctx.getExitCode() == 0); }
From source file:com.streamsets.pipeline.stage.destination.redis.RedisTarget.java
private void doPublish(Batch batch) throws StageException { Iterator<Record> records = batch.getRecords(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (records.hasNext()) { Record record = records.next();//www. j a v a2 s.c o m baos.reset(); try { DataGenerator generator = this.generatorFactory.getGenerator(baos); generator.write(record); generator.close(); for (String channel : conf.channel) { jedis.publish(channel, baos.toString()); } } catch (JedisConnectionException ex) { throw new StageException(Errors.REDIS_06, ex.toString(), ex); } catch (IOException ex) { LOG.error(Errors.REDIS_04.getMessage(), conf.dataFormat.getLabel(), record); errorRecordHandler.onError( new OnRecordErrorException(record, Errors.REDIS_04, conf.dataFormat.getLabel(), record)); } } }
From source file:com.qjdchina.qjdsale.MemberPictureFragment.java
private Bitmap compressImage(Bitmap image, int size) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//?100???baos int options = 100; while (baos.toByteArray().length / 1024 > size) { //??100kb, baos.reset();//?baos?baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//options%??baos options -= 10;//??10 }/*w w w .j av a2 s. co m*/ ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//??baosByteArrayInputStream Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//ByteArrayInputStream?? return bitmap; }
From source file:org.jboss.as.test.integration.management.cli.CliVariablesTestCase.java
/** * Tests variable chaining: set var2=$var1 * @throws Exception/* ww w . j a va 2s .c o m*/ */ @Test public void testVariableChaining() throws Exception { final String VAR1_NAME = "variable_1"; final String VAR2_NAME = "variable_2"; final String VAR1_VALUE = "value_1"; final String VAR2_VALUE = "$" + VAR1_NAME; final ByteArrayOutputStream cliOut = new ByteArrayOutputStream(); final CommandContext ctx = CLITestUtil.getCommandContext(cliOut); ctx.handle("set " + VAR1_NAME + "=" + VAR1_VALUE); ctx.handle("set " + VAR2_NAME + "=" + VAR2_VALUE); cliOut.reset(); ctx.handle("echo $" + VAR1_NAME); ctx.handle("echo $" + VAR2_NAME); String echoResult = cliOut.toString(); assertTrue(echoResult.contains(VAR1_VALUE)); assertFalse(echoResult.contains(VAR2_VALUE)); assertTrue(StringUtils.countMatches(echoResult, VAR1_VALUE) == 2); cliOut.reset(); ctx.handle("set"); //print all variables String setResult = cliOut.toString(); assertTrue(setResult.contains(VAR1_NAME + "=" + VAR1_VALUE)); assertTrue(setResult.contains(VAR2_NAME + "=" + VAR1_VALUE)); assertTrue(ctx.getExitCode() == 0); }
From source file:org.jboss.arquillian.ce.adapter.AbstractOpenShiftAdapter.java
public <T> T jolokia(Class<T> expectedReturnType, String podName, Object input) throws Exception { if (input instanceof J4pRequest == false) { throw new IllegalArgumentException("Input must be a J4pRequest instance!"); }//from w w w .j a v a 2 s . com Proxy proxy = getProxy(); String url = proxy.url(podName, "https", 8778, "/jolokia/", null); log.info(String.format("Jolokia URL: %s", url)); J4pRequest request = (J4pRequest) input; JSONObject jsonObject = ReflectionUtils.invoke(J4pRequest.class, "toJson", new Class[0], request, new Object[0], JSONObject.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStreamWriter out = new OutputStreamWriter(baos)) { jsonObject.writeJSONString(out); out.flush(); } byte[] bytes = baos.toByteArray(); // copy baos.reset(); // re-use try (InputStream stream = proxy.post(url, "application/json", bytes)) { byte[] buffer = new byte[512]; int numRead; while ((numRead = stream.read(buffer, 0, buffer.length)) >= 0) { baos.write(buffer, 0, numRead); } } String content = baos.toString(); JSONParser parser = new JSONParser(); JSONAware parseResult; try { parseResult = (JSONAware) parser.parse(content); } catch (ParseException e) { throw new IllegalArgumentException("Invalid Jolokia response: " + content); } if (parseResult instanceof JSONObject == false) { throw new IllegalStateException("Invalid JSON answer for a single request (expected a map but got a " + parseResult.getClass() + ")"); } J4pResponse response = ReflectionUtils.invoke(J4pRequest.class, "createResponse", new Class[] { JSONObject.class }, request, new Object[] { parseResult }, J4pResponse.class); return expectedReturnType.cast(response.getValue()); }
From source file:org.perf4j.LogParserTest.java
public void testLogParserMain() throws Exception { PrintStream realOut = System.out; ByteArrayOutputStream fakeOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(fakeOut, true)); try {/*from w w w. j a va 2s . co m*/ //usage realOut.println("-- Usage Test --"); LogParser.runMain(new String[] { "--help" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("Usage") >= 0); fakeOut.reset(); //log on std in, write to std out InputStream realIn = System.in; ByteArrayInputStream fakeIn = new ByteArrayInputStream(testLog.getBytes()); System.setIn(fakeIn); try { realOut.println("-- Std in -> Std out Test --"); LogParser.runMain(new String[0]); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); } finally { System.setIn(realIn); } //Log from a file FileUtils.writeStringToFile(new File("./target/logParserTest.log"), testLog); //log from file, write to std out realOut.println("-- File in -> Std out Test --"); LogParser.runMain(new String[] { "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); //CSV format test realOut.println("-- File in -> Std out Test with CSV --"); LogParser.runMain(new String[] { "-f", "csv", "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("\"tag\",") >= 0 && fakeOut.toString().indexOf("\"tag2\",") >= 0 && fakeOut.toString().indexOf("\"tag3\",") >= 0); fakeOut.reset(); //log from file, write to file realOut.println("-- File in -> File out Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "./target/logParserTest.log" }); String statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //log from file, write to file, different timeslice realOut.println("-- File in -> File out with different timeslice Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "--timeslice", "120000", "./target/logParserTest.log" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //missing param test realOut.println("-- Missing param test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "-o" })); //unknown arg test realOut.println("-- Unknown arg test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "--foo" })); realOut.println(fakeOut); assertTrue(fakeOut.toString().indexOf("Unknown") >= 0); //graphing test realOut.println("-- File in -> File out with graphing --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "-g", "./target/perfGraphs.out", "./src/test/resources/org/perf4j/dummyLog.txt" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); String graphsOut = FileUtils.readFileToString(new File("./target/perfGraphs.out")); realOut.println(graphsOut); assertTrue(graphsOut.indexOf("chtt=TPS") > 0 && graphsOut.indexOf("chtt=Mean") > 0); } finally { System.setOut(realOut); } }
From source file:net.jperf.LogParserTest.java
public void testLogParserMain() throws Exception { PrintStream realOut = System.out; ByteArrayOutputStream fakeOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(fakeOut, true)); try {/*from ww w.j av a 2 s . c o m*/ //usage realOut.println("-- Usage Test --"); LogParser.runMain(new String[] { "--help" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("Usage") >= 0); fakeOut.reset(); //log on std in, write to std out InputStream realIn = System.in; ByteArrayInputStream fakeIn = new ByteArrayInputStream(testLog.getBytes()); System.setIn(fakeIn); try { realOut.println("-- Std in -> Std out Test --"); LogParser.runMain(new String[0]); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); } finally { System.setIn(realIn); } //Log from a file FileUtils.writeStringToFile(new File("./target/logParserTest.log"), testLog); //log from file, write to std out realOut.println("-- File in -> Std out Test --"); LogParser.runMain(new String[] { "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0 && fakeOut.toString().indexOf("tag3") >= 0); fakeOut.reset(); //CSV format test realOut.println("-- File in -> Std out Test with CSV --"); LogParser.runMain(new String[] { "-f", "csv", "./target/logParserTest.log" }); realOut.println(fakeOut.toString()); assertTrue(fakeOut.toString().indexOf("\"tag\",") >= 0 && fakeOut.toString().indexOf("\"tag2\",") >= 0 && fakeOut.toString().indexOf("\"tag3\",") >= 0); fakeOut.reset(); //log from file, write to file realOut.println("-- File in -> File out Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "./target/logParserTest.log" }); String statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //log from file, write to file, different timeslice realOut.println("-- File in -> File out with different timeslice Test --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "--timeslice", "120000", "./target/logParserTest.log" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); assertTrue( statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0); //missing param test realOut.println("-- Missing param test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "-o" })); //unknown arg test realOut.println("-- Unknown arg test --"); assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "--foo" })); realOut.println(fakeOut); assertTrue(fakeOut.toString().indexOf("Unknown") >= 0); //graphing test realOut.println("-- File in -> File out with graphing --"); LogParser.runMain(new String[] { "-o", "./target/statistics.out", "-g", "./target/perfGraphs.out", "./src/test/resources/net/jperf/dummyLog.txt" }); statsOut = FileUtils.readFileToString(new File("./target/statistics.out")); realOut.println(statsOut); String graphsOut = FileUtils.readFileToString(new File("./target/perfGraphs.out")); realOut.println(graphsOut); assertTrue(graphsOut.indexOf("chtt=TPS") > 0 && graphsOut.indexOf("chtt=Mean") > 0); } finally { System.setOut(realOut); } }
From source file:org.dswarm.xsd2jsonschema.model.test.JSRootTest.java
@Test public void testOverloadedRender() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final StringWriter writer = new StringWriter(); File tmpFile;/*from w w w .j av a2 s. c o m*/ final String render = obj.render(); doRenderTest(render); obj.render(BaseJSTest.om, outputStream); doRenderTest(outputStream); outputStream.reset(); obj.render(BaseJSTest.om, outputStream, JsonEncoding.UTF8); doRenderTest(outputStream); outputStream.reset(); obj.render(BaseJSTest.om, writer); doRenderTest(writer); outputStream.reset(); tmpFile = File.createTempFile("dmp-test", "tmp"); obj.render(BaseJSTest.om, tmpFile, JsonEncoding.UTF8); doRenderTest(BaseJSTest.om.readTree(tmpFile)); tmpFile.deleteOnExit(); final JsonFactory factory = BaseJSTest.om.getFactory(); obj.render(factory, outputStream); doRenderTest(outputStream); outputStream.reset(); obj.render(factory, outputStream, JsonEncoding.UTF8); doRenderTest(outputStream); outputStream.reset(); obj.render(factory, writer); doRenderTest(writer); outputStream.reset(); tmpFile = File.createTempFile("dmp-test", "tmp"); obj.render(factory, tmpFile, JsonEncoding.UTF8); doRenderTest(BaseJSTest.om.readTree(tmpFile)); tmpFile.deleteOnExit(); final JsonGenerator generator = factory.createGenerator(outputStream); obj.render(generator); generator.flush(); doRenderTest(outputStream); }
From source file:org.apache.olingo.fit.utils.FSManager.java
public void putInMemory(final ResWrap<Entity> container, final String relativePath) throws IOException, ODataSerializerException { ByteArrayOutputStream content = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING); new AtomSerializer(true).write(writer, container); writer.flush();//from w w w . j a va 2 s.c om putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM)); content.reset(); new JsonSerializer(true, ContentType.JSON_FULL_METADATA).write(writer, container); writer.flush(); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA)); }
From source file:eu.smartfp7.jcouchdb.SmartContinuousChangesDriver.java
@Override public void run() { synchronized (this) { this.notifyAll(); }//from www . j a v a2s .c o m log.info("ContinuousChangesDriver started."); Response response = null; options = db.getCommonChangesOptions(filter, since, options); options.put("feed", "continuous"); InputStream ir = null; while (!db.getServer().isShutdown()) { log.debug("driver loop"); try { log.info("Sending continuous change request. filter = {}, since = {}, options = {}", new Object[] { filter, since, options }); response = db.getServer().get("/" + db.getName() + "/_changes" + options.toQuery()); ir = response.getInputStream(); log.debug("input stream = {}", ir); ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (!db.getServer().isShutdown()) { int c = ir.read(); if (c == -1) { throw new IllegalStateException("Unexpected EOF"); } if (c == '\n') { convertRawData(bos.toByteArray()); bos.reset(); } else { bos.write(c); } } } catch (SocketException e) { log.debug("SocketException listening to continuous changes"); } catch (Exception e) { log.info("Error listening to continuous changes", e); } finally { if (ir != null) { IOUtils.closeQuietly(ir); ir = null; } if (response != null) { response.destroy(); } } } }