public ByteBuf getByteBuf() { final ByteBuf copy = Unpooled.copiedBuffer(baos.toByteArray()); baos.reset(); return copy; }
private static List<byte[]> parseParentsBytes(byte[] bytes) { List<byte[]> parents = new ArrayList<>(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == ESCAPE_BYTE) { i++; if (bytes[i] == SEPARATED_BYTE) { parents.add(bos.toByteArray()); bos.reset(); continue; } // fall through to append the byte } bos.write(bytes[i]); } if (bos.size() > 0) { parents.add(bos.toByteArray()); } return parents; }
private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 BitmapFactory.Options options1 = new BitmapFactory.Options(); options1.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, options1);//把ByteArrayInputStream数据生成图片 return bitmap; }
@Test public void testCommandNotFound() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream(baos); System.setOut(stream); itf.execute("missing"); assertThat(baos.toString()).contains("The command", "missing", "See", "--help"); baos.reset(); itf.execute("missing", "--help"); assertThat(baos.toString()).contains("The command", "missing", "See", "--help"); }
private void processCOMMAND(final byte b) { if (b == RELPFrame.SEPARATOR) { final String command = new String(currBytes.toByteArray(), charset); frameBuilder.command(command); logger.debug("Command is {}", new Object[] {command}); currBytes.reset(); currState = RELPState.LENGTH; } else { currBytes.write(b); } }
/** * 使用StringBuilder前,务必调用 */ @Override public void flush() throws IOException { if (null != baos) { baos.flush(); if (baos.size() > 0) { if (charset == null) sb.append(new String(baos.toByteArray())); else sb.append(new String(baos.toByteArray(), charset)); baos.reset(); } } }
final java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream(); ref.reset(); written = baout.write(new ByteArrayInputStream(ref.toByteArray())); assertEquals(155, written); checkStreams(baout, ref); final java.io.ByteArrayOutputStream ref1 = new java.io.ByteArrayOutputStream(); baout.writeTo(ref1); checkStreams(baout1, ref1); final String refString = ref.toString("ASCII"); assertEquals("ASCII decoded String must be equal", refString, baoutString);
private ByteArrayOutputStream getOutputBuffer(int suggestedLength) { if (null == outputBuffer) { outputBuffer = new ByteArrayOutputStream(suggestedLength); } outputBuffer.reset(); return outputBuffer; } }
@Override protected synchronized void writeHeader(final long firstEventId, final DataOutputStream out) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); eventSchema.writeTo(baos); out.writeInt(baos.size()); baos.writeTo(out); baos.reset(); headerSchema.writeTo(baos); out.writeInt(baos.size()); baos.writeTo(out); this.firstEventId = firstEventId; this.systemTimeOffset = System.currentTimeMillis(); final Map<String, Object> headerValues = new HashMap<>(); headerValues.put(EventIdFirstHeaderSchema.FieldNames.FIRST_EVENT_ID, firstEventId); headerValues.put(EventIdFirstHeaderSchema.FieldNames.TIMESTAMP_OFFSET, systemTimeOffset); headerValues.put(EventIdFirstHeaderSchema.FieldNames.COMPONENT_IDS, idLookup.getComponentIdentifiers()); headerValues.put(EventIdFirstHeaderSchema.FieldNames.COMPONENT_TYPES, idLookup.getComponentTypes()); headerValues.put(EventIdFirstHeaderSchema.FieldNames.QUEUE_IDS, idLookup.getQueueIdentifiers()); headerValues.put(EventIdFirstHeaderSchema.FieldNames.EVENT_TYPES, eventTypeNames); final FieldMapRecord headerInfo = new FieldMapRecord(headerSchema, headerValues); schemaRecordWriter.writeRecord(headerInfo, out); }
@Override public String read() throws IOException { String message = Strings.EMPTY; try { while (true) { final int b = inputStream.read(); if (b == -1) { throw new EOFException("The stream has been closed or the end of stream has been reached"); } buffer.write(b); if (b == '\n') { break; } } } catch (final EOFException e) { if (buffer.size() > 0) { message = buffer.toString(); buffer.reset(); return message; } throw e; } message = buffer.toString(); buffer.reset(); return message; } }
private static int readInputLine(final ByteArrayOutputStream bOut, int lookAhead, final InputStream fIn) throws IOException { bOut.reset(); int ch = lookAhead; do { bOut.write(ch); if (ch == '\r' || ch == '\n') { lookAhead = readPassedEOL(bOut, ch, fIn); break; } } while ((ch = fIn.read()) >= 0); return lookAhead; }
static int getObjectSize(Object object) { if (!(object instanceof Serializable)) { return -1; } final Serializable serializable = (Serializable) object; // synchronized pour protéger l'accès à TEMP_OUTPUT static synchronized (TEMP_OUTPUT) { TEMP_OUTPUT.reset(); try { final ObjectOutputStream out = new ObjectOutputStream(TEMP_OUTPUT); try { out.writeObject(serializable); } finally { out.close(); } return TEMP_OUTPUT.size(); } catch (final Throwable e) { // NOPMD // ce catch Throwable inclut IOException et aussi NoClassDefFoundError/ClassNotFoundException (issue 355) return -1; } } } }
private void processDATA(final byte b) { currBytes.write(b); logger.trace("Data size is {}", new Object[] {currBytes.size()}); if (currBytes.size() >= frameBuilder.dataLength) { final byte[] data = currBytes.toByteArray(); frameBuilder.data(data); logger.debug("Reached expected data size of {}", new Object[] {frameBuilder.dataLength}); currBytes.reset(); currState = RELPState.TRAILER; } }
ByteArrayOutputStream buf = new ByteArrayOutputStream(); OutputStreamWriter w = new OutputStreamWriter(buf, StandardCharsets.UTF_8); for (byte b : buf.toByteArray()) { out.append('%'); out.append(toDigit((b >> 4) & 0xF)); out.append(toDigit(b & 0xF)); buf.reset(); escaped = true;
public void testVerbosePrintNullLabelAndMap() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final PrintStream outPrint = new PrintStream(out); outPrint.println("null"); final String EXPECTED_OUT = out.toString(); out.reset(); MapUtils.verbosePrint(outPrint, null, null); assertEquals(EXPECTED_OUT, out.toString()); }
protected String readString(DataInputStream input,int pos,int[] reads) throws IOException { int read=reads[0]; input.skip(pos-read); read=pos; output.reset(); while(true) { int c1 = input.read(); int c2 = input.read(); read+=2; if(c1==0 && c2==0) { break; } else { output.write(c1); output.write(c2); } } reads[0]=read; return new String(output.toByteArray(),encoding); }
private void handleFrame() { byte[] bytes = this.outputStream.toByteArray(); this.outputStream.reset(); String content = new String(bytes, SockJsFrame.CHARSET); if (logger.isTraceEnabled()) { logger.trace("XHR content received: " + content); } if (!PRELUDE.equals(content)) { this.sockJsSession.handleFrame(new String(bytes, SockJsFrame.CHARSET)); } }