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

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

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

ByteArrayOutputStream.<init>介绍

[英]Constructs a new ByteArrayOutputStream with a default size of 32 bytes. If more than 32 bytes are written to this instance, the underlying byte array will expand.
[中]构造默认大小为32字节的新ByteArrayOutputStream。如果写入此实例的字节数超过32个,则基础字节数组将展开。

代码示例

代码示例来源:origin: stackoverflow.com

ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
  result.write(buffer, 0, length);
}
return result.toString("UTF-8");

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

@Override
public String convert(Properties source) {
  try {
    ByteArrayOutputStream os = new ByteArrayOutputStream(256);
    source.store(os, null);
    return os.toString("ISO-8859-1");
  }
  catch (IOException ex) {
    // Should never happen.
    throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex);
  }
}

代码示例来源:origin: bumptech/glide

public static byte[] isToBytes(InputStream is) throws IOException {
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 byte[] buffer = new byte[1024];
 int read;
 try {
  while ((read = is.read(buffer)) != -1) {
   os.write(buffer, 0, read);
  }
 } finally {
  is.close();
 }
 return os.toByteArray();
}

代码示例来源:origin: google/guava

/** Serializes and deserializes the specified object. */
@SuppressWarnings("unchecked")
static <T> T reserialize(T object) {
 checkNotNull(object);
 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 try {
  ObjectOutputStream out = new ObjectOutputStream(bytes);
  out.writeObject(object);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
  return (T) in.readObject();
 } catch (IOException | ClassNotFoundException e) {
  throw new RuntimeException(e);
 }
}

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

private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    serialization.serialize(storeValue, out);
    return out.toByteArray();
  }
  finally {
    out.close();
  }
}

代码示例来源:origin: alibaba/canal

public static byte[] readNullTerminatedBytes(byte[] data, int index) {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  for (int i = index; i < data.length; i++) {
    byte item = data[i];
    if (item == MSC.NULL_TERMINATED_STRING_DELIMITER) {
      break;
    }
    out.write(item);
  }
  return out.toByteArray();
}

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

private void transformAndMarshal(Object graph, Result result) throws IOException {
  try {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    marshalOutputStream(graph, os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    Transformer transformer = this.transformerFactory.newTransformer();
    transformer.transform(new StreamSource(is), result);
  }
  catch (TransformerException ex) {
    throw new MarshallingFailureException(
        "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
  }
}

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

@Test
public void testMissingValue() {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 PrintStream stream = new PrintStream(baos);
 System.setOut(stream);
 itf.execute("hello", "-n");
 assertThat(baos.toString())
   .contains("The option 'name' requires a value");
}

代码示例来源:origin: alibaba/arthas

public static String toString(InputStream inputStream) throws IOException {
  ByteArrayOutputStream result = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int length;
  while ((length = inputStream.read(buffer)) != -1) {
    result.write(buffer, 0, length);
  }
  return result.toString("UTF-8");
}

代码示例来源:origin: google/guava

@GwtIncompatible // java serialization not supported in GWT.
private static byte[] serializeWithBackReference(Object original, int handleOffset)
  throws IOException {
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 ObjectOutputStream out = new ObjectOutputStream(bos);
 out.writeObject(original);
 byte[] handle = toByteArray(baseWireHandle + handleOffset);
 byte[] ref = prepended(TC_REFERENCE, handle);
 bos.write(ref);
 return bos.toByteArray();
}

代码示例来源:origin: stackoverflow.com

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
 out = new ObjectOutputStream(bos);   
 out.writeObject(yourObject);
 out.flush();
 byte[] yourBytes = bos.toByteArray();
 ...
} finally {
 try {
  bos.close();
 } catch (IOException ex) {
  // ignore close exception
 }
}

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

/**
 * Attempts to lift the security restriction on the underlying channel.
 * This requires the administer privilege on the server.
 *
 * @throws SecurityException
 *      If we fail to upgrade the connection.
 */
public void upgrade() {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  if (execute(Arrays.asList("groovy", "="),
      new ByteArrayInputStream("hudson.remoting.Channel.current().setRestricted(false)".getBytes()),
      out,out)!=0)
    throw new SecurityException(out.toString()); // failed to upgrade
}

代码示例来源:origin: skylot/jadx

public byte[] convert(String javaFile) throws JadxException {
  try (ByteArrayOutputStream out = new ByteArrayOutputStream();
     ByteArrayOutputStream errOut = new ByteArrayOutputStream()) {
    DxContext context = new DxContext(out, errOut);
    DxArgs args = new DxArgs(context, "-", new String[]{javaFile});
    int result = (new Main(context)).runDx(args);
    dxErrors = errOut.toString(CHARSET_NAME);
    if (result != 0) {
      throw new JadxException("Java to dex conversion error, code: " + result);
    }
    return out.toByteArray();
  } catch (Exception e) {
    throw new JadxException("dx exception: " + e.getMessage(), e);
  }
}

代码示例来源:origin: stackoverflow.com

HttpClient httpclient = new DefaultHttpClient();
 HttpResponse response = httpclient.execute(new HttpGet(URL));
 StatusLine statusLine = response.getStatusLine();
 if(statusLine.getStatusCode() == HttpStatus.SC_OK){
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   response.getEntity().writeTo(out);
   String responseString = out.toString();
   out.close();
   //..more logic
 } else{
   //Closes the connection.
   response.getEntity().getContent().close();
   throw new IOException(statusLine.getReasonPhrase());
 }

代码示例来源:origin: google/guava

@SuppressWarnings("unchecked")
static <T> T reserialize(T object) {
 try {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(bytes);
  out.writeObject(object);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
  return (T) in.readObject();
 } catch (IOException | ClassNotFoundException e) {
  Helpers.fail(e, e.getMessage());
 }
 throw new AssertionError("not reachable");
}

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

private static byte[] readAllBytes(InputStream input) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  int numRead;
  byte[] buffer = new byte[16384];
  while ((numRead = input.read(buffer, 0, buffer.length)) != -1) {
    out.write(buffer, 0, numRead);
  }
  return out.toByteArray();
}

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

/**
 * @return a byte[] containing the information contained in the
 * specified InputStream.
 * @throws java.io.IOException
 */
public static byte[] getBytes(InputStream input)
  throws IOException {
 ByteArrayOutputStream result = new ByteArrayOutputStream();
 copy(input, result);
 result.close();
 return result.toByteArray();
}

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

private String readCommand(ByteBuffer byteBuffer) {
  ByteArrayOutputStream command = new ByteArrayOutputStream(256);
  while (byteBuffer.remaining() > 0 && !tryConsumeEndOfLine(byteBuffer)) {
    command.write(byteBuffer.get());
  }
  return new String(command.toByteArray(), StandardCharsets.UTF_8);
}

代码示例来源:origin: google/guava

public void testCopyChannel() throws IOException {
 byte[] expected = newPreFilledByteArray(100);
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 WritableByteChannel outChannel = Channels.newChannel(out);
 ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected));
 ByteStreams.copy(inChannel, outChannel);
 assertEquals(expected, out.toByteArray());
}

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

@Test
public void testMissingOption() {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 PrintStream stream = new PrintStream(baos);
 System.setOut(stream);
 itf.execute("hello");
 assertThat(baos.toString())
   .contains("The option", "name", "is required");
}

相关文章