public byte[] serialize(GenericRecord record) throws IOException { this.outputStream.reset(); this.writer.write(record, this.encoder); this.encoder.flush(); return this.outputStream.toByteArray(); } }
@Test public void testToInputStreamWithReset() throws IOException { //Make sure reset() do not destroy InputStream returned from toInputStream() final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); //Write 8224 bytes writeData(baout, ref, 32); for(int i=0;i<128;i++) { writeData(baout, ref, 64); } //Get data before reset final InputStream in = baout.toInputStream(); byte refData[] = ref.toByteArray(); //Reset and write some new data baout.reset(); ref.reset(); writeData(baout, ref, new int[] { 2, 4, 8, 16 }); //Check original data byte baoutData[] = IOUtils.toByteArray(in); assertEquals(8224, baoutData.length); checkByteArrays(refData, baoutData); //Check new data written after reset baoutData = IOUtils.toByteArray(baout.toInputStream()); refData = ref.toByteArray(); assertEquals(30, baoutData.length); checkByteArrays(refData, baoutData); baout.close(); in.close(); }
private void newControlSequence() { baout.reset(); }
@Override public void passivateObject(PooledObject<ByteArrayOutputStream> pooledObject) { pooledObject.getObject().reset(); } },config);
@Override public void clear() { this.baos.reset(); this.cos.resetByteCount(); }
@Override public void clear() { this.baos.reset(); this.cos.resetByteCount(); }
public ChartInfo ( ) { byteArrayOutputStream = new ByteArrayOutputStream( 64 * 1024 ); // Grows // as // needed byteArrayOutputStream.reset( ); dataCollectionForTheChart = new TimeSeriesCollection( ); rowData = ""; }
/** * Plots the chart and sends it in the form of ByteArrayOutputStream to * outside. * * @return Returns the byteArrayOutputStream. */ public synchronized ByteArrayOutputStream writePlot ( ) { if ( !changed ) return byteArrayOutputStream; byteArrayOutputStream.reset( ); try { ChartUtilities.writeChartAsPNG( byteArrayOutputStream , chart , width , height , false , 8 ); } catch ( IOException e ) { logger.warn( e.getMessage( ) , e ); } return byteArrayOutputStream; }
private void runScript(Path scriptPath) throws MongoDataException { String script; try { ProcessBuilder mongoProcess = new ProcessBuilder(getCommands(scriptPath)); Process process=mongoProcess.start(); int result=process.waitFor(); String stdOut; String errOut; try(ByteArrayOutputStream out = new ByteArrayOutputStream()){ IOUtils.copy(process.getInputStream(),out); stdOut = new String(out.toByteArray(),"UTF-8"); out.reset(); IOUtils.copy(process.getErrorStream(),out); errOut = new String(out.toByteArray(),"UTF-8"); IOUtils.copy(process.getInputStream(),out); } if(result!=0){ throw new IOException("Process return error \n std out:"+stdOut+"\n err out: \n"+errOut); }else{ logger.debug("Process return \n std out:"+stdOut+"\n err out: \n"+errOut); } } catch (IOException | InterruptedException ex) { logger.error("Unable to Execute mongo Process", ex); } }
private synchronized void log(byte event, long trxNumber) throws DBException { try { if (stream == null || !stream.getChannel().isOpen()) { this.stream = new FileOutputStream( config.journalDir() + File.separator + "transaction.journal", /* append= */true); } bos.reset(); CRC32 checksum = new CRC32(); dos.write(event); dos.writeLong(trxNumber); byte[] b = bos.toByteArray(); checksum.update(b); dos.writeInt((int) checksum.getValue()); stream.write(bos.toByteArray()); stream.flush(); } catch (IOException e) { throw new DBException(e); } }
public void run ( ) { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(1024*20); byte[] buffer = new byte[16*1024]; BufferedInputStream content; while ( isActive( ) ) { try { Thread.sleep( rate ); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); if ( httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED ) continue; content = new BufferedInputStream(httpURLConnection.getInputStream(),4096); arrayOutputStream.reset(); int readIndex = -1; while ( (readIndex= content.read(buffer))!=-1) arrayOutputStream.write(buffer, 0, readIndex); postStreamElement( arrayOutputStream.toByteArray()); } catch ( InterruptedException e ) { logger.error( e.getMessage( ) , e ); }catch (IOException e) { logger.error( e.getMessage( ) , e ); } } } public String getWrapperName() {
outputStream.reset( ); byte [ ] rawData = ( byte [ ] ) data.getData( "IMAGE" ); input = new ByteArrayInputStream( rawData );
public RBlockState(Algorithm compressionAlgo, FSDataInputStream fsin, BlockRegion region, Configuration conf, Reader r) throws IOException { this.compressAlgo = compressionAlgo; Decompressor decompressor = compressionAlgo.getDecompressor(); this.region = region; try { InputStream in = compressAlgo.createDecompressionStream(new BoundedRangeFileInputStream(fsin, region.getOffset(), region.getCompressedSize()), decompressor, DTFile.getFSInputBufferSize(conf)); int l = 1; r.baos.reset(); byte[] buf = new byte[DTFile.getFSInputBufferSize(conf)]; while (l >= 0) { l = in.read(buf); if (l > 0) { r.baos.write(buf, 0, l); } } // keep decompressed data into cache byte[] blockData = r.baos.toByteArray(); rbain = new ReusableByteArrayInputStream(blockData); } catch (IOException e) { compressAlgo.returnDecompressor(decompressor); throw e; } }