org.apache.commons.io.output.ByteArrayOutputStream.write()方法的使用及代码示例

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

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

ByteArrayOutputStream.write介绍

[英]Write a byte to byte array.
[中]写一个字节到字节的数组。

代码示例

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

@Override
public int read(byte b[], int off, int len) throws IOException {
  int r = in.read(b, off, len);
  if(r>0) {
    int sp = space();
    if(sp>0)
      side.write(b,off,Math.min(r, sp));
  }
  return r;
}

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

private ByteArrayOutputStream encodeToBytes() throws IOException {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  try (OutputStream gzos = new GZIPOutputStream(buf);
     ObjectOutputStream oos = JenkinsJVM.isJenkinsJVM() ? AnonymousClassWarnings.checkingObjectOutputStream(gzos) : new ObjectOutputStream(gzos)) {
    oos.writeObject(this);
  }
  ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2,true,-1,null));
  try {
    buf2.write(PREAMBLE);
    if (JenkinsJVM.isJenkinsJVM()) { // else we are in another JVM and cannot sign; result will be ignored unless INSECURE
      byte[] mac = MAC.mac(buf.toByteArray());
      dos.writeInt(- mac.length); // negative to differentiate from older form
      dos.write(mac);
    }
    dos.writeInt(buf.size());
    buf.writeTo(dos);
  } finally {
    dos.close();
  }
  buf2.write(POSTAMBLE);
  return buf2;
}

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

@Override
  public void write(final byte[] ba) throws IOException {
    if (ba != null){
      super.write(ba);
    }
  }
};

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

written = baout.write(new ByteArrayInputStream(ref.toByteArray()));
assertEquals(155, written);
checkStreams(baout, ref);

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

@Override
public int read() throws IOException {
  int i = in.read();
  if(i>=0 && space()>0)
    side.write(i);
  return i;
}

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

@Override
  public void write(final byte[] b, final int off, final int len) {
    numWrites.incrementAndGet();
    super.write(b, off, len);
  }
};

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

/**
 * Fetches entire contents of an <code>InputStream</code> and represent
 * same data as result InputStream.
 * <p>
 * This method is useful where,
 * <ul>
 * <li>Source InputStream is slow.</li>
 * <li>It has network resources associated, so we cannot keep it open for
 * long time.</li>
 * <li>It has network timeout associated.</li>
 * </ul>
 * It can be used in favor of {@link #toByteArray()}, since it
 * avoids unnecessary allocation and copy of byte[].<br>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input Stream to be fully buffered.
 * @param size the initial buffer size
 * @return A fully buffered stream.
 * @throws IOException if an I/O error occurs
 * @since 2.5
 */
public static InputStream toBufferedInputStream(final InputStream input, final int size)
    throws IOException {
  // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
  @SuppressWarnings("resource")
  final ByteArrayOutputStream output = new ByteArrayOutputStream(size);
  output.write(input);
  return output.toInputStream();
}

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

private int writeData(final ByteArrayOutputStream baout,
      final java.io.ByteArrayOutputStream ref,
      final int count) {
  if (count > DATA.length) {
    throw new IllegalArgumentException("Requesting too many bytes");
  }
  if (count == 0) {
    baout.write(100);
    ref.write(100);
    return 1;
  } else {
    baout.write(DATA, 0, count);
    ref.write(DATA, 0, count);
    return count;
  }
}

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

@Test
  public void testWriteNullBaSucceeds() throws Exception {
    final byte[] ba = null;
    original.write(ba);
    proxied.write(ba);
  }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() throws IOException {
 ByteArrayOutputStream buf = new ByteArrayOutputStream();
 buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
 buf.write(0);
 // type 2 indicator
 buf.write(3);
 buf.write(0);
 buf.write(0);
 buf.write(0);
 buf.write("challenge".getBytes());
 NtlmEngine engine = new NtlmEngine();
 engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray()));
 buf.close();
 fail("An NtlmEngineException must have occurred as type 2 indicator is incorrect");
}

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

public HttpResponse sendResponse(HttpResponseStatus responseCode, String responseBody) {
  String actualResponseBody = responseBody + "\n";
  HttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseCode);
  response.setHeader(CONTENT_LENGTH, actualResponseBody.length());
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  try {
    outputStream.write(actualResponseBody.getBytes());
  } catch(IOException e) {
    logger.error("IOException while trying to write the outputStream for an admin response", e);
    throw new RuntimeException(e);
  }
  ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer();
  responseContent.writeBytes(outputStream.toByteArray());
  response.setContent(responseContent);
  if (logger.isDebugEnabled()) {
    logger.debug("Sent " + response);
  }
  return response;
}

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

blob = new ByteArrayOutputStream(n);
blob.write(buf, 0, n);
blobLength += n;
if (blobLength >= maxBlobLength) {

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() throws IOException {
 ByteArrayOutputStream buf = new ByteArrayOutputStream();
 buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
 buf.write(0);
 // type 2 indicator
 buf.write(2);
 buf.write(0);
 buf.write(0);
 buf.write(0);
 buf.write(longToBytes(1L)); // we want to write a Long
 // flags
 buf.write(0);// unicode support indicator
 buf.write(0);
 buf.write(0);
 buf.write(0);
 buf.write(longToBytes(1L));// challenge
 NtlmEngine engine = new NtlmEngine();
 engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray()));
 buf.close();
 fail("An NtlmEngineException must have occurred as unicode support is not indicated");
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void testGenerateType3Msg() throws IOException {
 ByteArrayOutputStream buf = new ByteArrayOutputStream();
 buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
 buf.write(0);
 // type 2 indicator
 buf.write(2);
 buf.write(0);
 buf.write(0);
 buf.write(0);
 buf.write(longToBytes(0L)); // we want to write a Long
 // flags
 buf.write(1);// unicode support indicator
 buf.write(0);
 buf.write(0);
 buf.write(0);
 buf.write(longToBytes(1L));// challenge
 NtlmEngine engine = new NtlmEngine();
 String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation", 
     Base64.getEncoder().encodeToString(buf.toByteArray()));
 buf.close();
 assertEquals(
     type3Msg,
     "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=",
     "Incorrect type3 message generated");
}

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

blob = new ByteArrayOutputStream(n);
blob.write(buf, 0, n);
blobLength += n;
if (blobLength >= maxBlobLength) {

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

@Test
public void testClose() throws IOException {
  shielded.close();
  assertFalse("closed", closed);
  try {
    shielded.write('x');
    fail("write(b)");
  } catch (final IOException ignore) {
    // expected
  }
  original.write('y');
  assertEquals(1, original.size());
  assertEquals('y', original.toByteArray()[0]);
}

代码示例来源:origin: palantir/atlasdb

@Test
public void testAddDelete() throws Exception {
  final byte[] data = PtBytes.toBytes("streamed");
  final long streamId = txManager.runTaskWithRetry((TransactionTask<Long, Exception>) t -> {
    Sha256Hash hash = Sha256Hash.computeHash(data);
    byte[] reference = "ref".getBytes();
    return defaultStore.getByHashOrStoreStreamAndMarkAsUsed(t, hash, new ByteArrayInputStream(data), reference);
  });
  txManager.runTaskWithRetry((TransactionTask<Void, Exception>) t -> {
    Optional<InputStream> inputStream = defaultStore.loadSingleStream(t, streamId);
    assertTrue(inputStream.isPresent());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(inputStream.get());
    assertArrayEquals(data, outputStream.toByteArray());
    return null;
  });
}

代码示例来源:origin: org.jenkins-ci.main/jenkins-core

@Override
public int read(byte b[], int off, int len) throws IOException {
  int r = in.read(b, off, len);
  if(r>0) {
    int sp = space();
    if(sp>0)
      side.write(b,off,Math.min(r, sp));
  }
  return r;
}

代码示例来源:origin: org.jenkins-ci.main/jenkins-core

@Override
public int read() throws IOException {
  int i = in.read();
  if(i>=0 && space()>0)
    side.write(i);
  return i;
}

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

certDataStream.write(sslCert.getCert().getBytes());

相关文章