java.io.ByteArrayOutputStream.reset()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(247)

本文整理了Java中java.io.ByteArrayOutputStream.reset()方法的一些代码示例,展示了ByteArrayOutputStream.reset()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteArrayOutputStream.reset()方法的具体详情如下:
包路径:java.io.ByteArrayOutputStream
类名称:ByteArrayOutputStream
方法名:reset

ByteArrayOutputStream.reset介绍

[英]Resets this stream to the beginning of the underlying byte array. All subsequent writes will overwrite any bytes previously stored in this stream.
[中]将此流重置为基础字节数组的开头。所有后续写入都将覆盖以前存储在此流中的任何字节。

代码示例

代码示例来源:origin: Netflix/zuul

public ByteBuf getByteBuf() {
  final ByteBuf copy = Unpooled.copiedBuffer(baos.toByteArray());
  baos.reset();
  return copy;
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public void flush() throws IOException {
  if (baos.size() > 0) {
    LogRecord lr = new LogRecord(level, baos.toString());
    lr.setLoggerName(logger.getName());
    lr.setSourceClassName(caller.getClassName());
    lr.setSourceMethodName(caller.getMethodName());
    logger.log(lr);
  }
  baos.reset();
}

代码示例来源:origin: libgdx/libgdx

public void endChunk (DataOutputStream target) throws IOException {
    flush();
    target.writeInt(buffer.size() - 4);
    buffer.writeTo(target);
    target.writeInt((int)crc.getValue());
    buffer.reset();
    crc.reset();
  }
}

代码示例来源:origin: apache/hbase

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;
}

代码示例来源:origin: qiujuer/Genius-Android

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;
}

代码示例来源:origin: eclipse-vertx/vert.x

@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");
}

代码示例来源:origin: apache/nifi

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);
  }
}

代码示例来源:origin: nutzam/nutz

/**
 * 使用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();
    }
  }
}

代码示例来源:origin: commons-io/commons-io

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);

代码示例来源:origin: apache/avro

private ByteArrayOutputStream getOutputBuffer(int suggestedLength) {
  if (null == outputBuffer) {
   outputBuffer = new ByteArrayOutputStream(suggestedLength);
  }
  outputBuffer.reset();
  return outputBuffer;
 }
}

代码示例来源:origin: apache/nifi

@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);
}

代码示例来源:origin: zendesk/maxwell

public String consume() throws IOException {
  jsonGenerator.flush();
  String s = buffer.toString();
  buffer.reset();
  return s;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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;
  }
}

代码示例来源:origin: floragunncom/search-guard

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;
}

代码示例来源:origin: javamelody/javamelody

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;
      }
    }
  }
}

代码示例来源:origin: apache/nifi

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;
  }
}

代码示例来源:origin: jenkinsci/jenkins

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;

代码示例来源:origin: commons-collections/commons-collections

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());
}

代码示例来源:origin: FudanNLP/fnlp

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);
}

代码示例来源:origin: spring-projects/spring-framework

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));
  }
}

相关文章