java.io.ByteArrayOutputStream类的使用及代码示例

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

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

ByteArrayOutputStream介绍

[英]A specialized OutputStream for class for writing content to an (internal) byte array. As bytes are written to this stream, the byte array may be expanded to hold more bytes. When the writing is considered to be finished, a copy of the byte array can be requested from the class.
[中]类的专用输出流,用于将内容写入(内部)字节数组。当字节写入该流时,字节数组可能会被扩展以容纳更多字节。当写入被认为已经完成时,可以从类中请求字节数组的副本。

代码示例

代码示例来源: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: 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: stackoverflow.com

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
  out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

代码示例来源: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

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: 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: qiurunze123/miaosha

public void run() {
    try {
      for(int i=0;i<10;i++) {
        URL url = new URL("http://192.168.220.130/index.html");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        InputStream in = conn.getInputStream();
        ByteArrayOutputStream bout  = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int len = 0;
        while((len = in.read(buff)) >= 0) {
          bout.write(buff, 0, len);
        }
        in.close();
        bout.close();
        byte[] response = bout.toByteArray();
        System.out.println(new String(response, "UTF-8"));
        Thread.sleep(3000);
      }
    }catch(Exception e) {
      
    }
  }
});

代码示例来源:origin: org.apache.poi/poi

public static byte[] decompress(byte[] compressed, int offset, int length) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream instream = new ByteArrayInputStream(compressed, offset, length);
    InputStream stream = new RLEDecompressingInputStream(instream);
    IOUtils.copy(stream, out);
    stream.close();
    out.close();
    return out.toByteArray();
  }
}

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

in = url.openStream();
out = new FileOutputStream(tmpFile);
if (TRY_TO_PATCH_SHADED_ID && PlatformDependent.isOsx() && !packagePrefix.isEmpty()) {
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(in.available());
  while ((length = in.read(buffer)) > 0) {
    byteArrayOutputStream.write(buffer, 0, length);
  byteArrayOutputStream.flush();
  byte[] bytes = byteArrayOutputStream.toByteArray();
  byteArrayOutputStream.close();
  out.write(bytes);
} else {
  while ((length = in.read(buffer)) > 0) {
    out.write(buffer, 0, length);
out.flush();

代码示例来源:origin: jmdhappy/xxpay-master

SSLSocketFactory ssf = sslContext.getSocketFactory();
  URL url = new URL(requestUrl);
  HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  conn.setSSLSocketFactory(ssf);
  if (null != outputStr) {
    OutputStream outputStream = conn.getOutputStream();
    outputStream.write(outputStr.getBytes("UTF-8"));
    outputStream.close();
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  byte[] buffer = new byte[4096];
  int n = 0;
  while (-1 != (n = inputStream.read(buffer))) {
    output.write(buffer, 0, n);
  return output.toByteArray();
} catch (Exception e) {
  e.printStackTrace();

代码示例来源:origin: googlemaps/android-maps-utils

protected byte[] doInBackground(String... params) {
  try {
    InputStream is =  new URL(mUrl).openStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = is.read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, nRead);
    }
    buffer.flush();
    return buffer.toByteArray();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

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

private static void doTestSerializationRoundTrip(DownloadAction action) throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 DataOutputStream output = new DataOutputStream(out);
 DownloadAction.serializeToStream(action, output);
 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 DataInputStream input = new DataInputStream(in);
 DownloadAction action2 =
   DownloadAction.deserializeFromStream(
     new DownloadAction.Deserializer[] {DashDownloadAction.DESERIALIZER}, input);
 assertThat(action).isEqualTo(action2);
}

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

buffer = new ByteArrayOutputStream();
  while ((nRead = in.read(data, 0, BUFFER_SIZE)) != -1) {
    buffer.write(data, 0, nRead);
  buffer.flush();
  return buffer.toByteArray();
} catch (IOException e) {
  e.printStackTrace();
  return null;
} finally {
  if (in != null) try { in.close(); } catch (Exception ignored) {}
  if (buffer != null) try { buffer.close(); } catch (Exception ignored) {}

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

public void testCreateAfterCloseShouldFail() throws Exception {
  for (int i = 0; i < 10; i++) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
        Ids.OPEN_ACL_UNSAFE, 1);
    createReq.serialize(boa, "request");
    baos.close();
    System.out.println("Length:" + baos.toByteArray().length);
    try {
      OutputStream outstream = sock.getOutputStream();
      byte[] data = baos.toByteArray();
      outstream.write(data);
      outstream.flush();
      while ((len = resultStream.read(b)) >= 0) {
        resultStream.close();

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

public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ObjectOutputStream oos = new ObjectOutputStream(baos);
  oos.writeObject(o);
  oos.flush();
  baos.flush();
  byte[] bytes = baos.toByteArray();
  ByteArrayInputStream is = new ByteArrayInputStream(bytes);
  ObjectInputStream ois = new ObjectInputStream(is);
  Object o2 = ois.readObject();
  return o2;
}

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

@SuppressWarnings("deprecation") // testing a deprecated method
public void testWriteBytes() throws IOException {
 /* Write out various test values in LITTLE ENDIAN FORMAT */
 out.writeBytes("r\u00C9sum\u00C9");
 byte[] data = baos.toByteArray();
 /* Setup input streams */
 DataInput in = new DataInputStream(new ByteArrayInputStream(data));
 /* Read in various values NORMALLY */
 byte[] b = new byte[6];
 in.readFully(b);
 assertEquals("r\u00C9sum\u00C9".getBytes(Charsets.ISO_8859_1), b);
}

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

public byte[] toByteArray() throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 List<Object> all = new LinkedList<>();
 synchronized (outList) {
  all.addAll(outList);
 }
 for (Object o : all) {
  if (o instanceof File) {
   File f = (File) o;
   FileInputStream fin = new FileInputStream(f);
   copyStream(fin, out);
   fin.close();
  } else if (o instanceof byte[]) {
   out.write((byte[]) o);
  } else if (o instanceof Integer) {
   out.write((int) o);
  } else if (o instanceof URL) {
   InputStream fin = ((URL) o).openStream();
   copyStream(fin, out);
   fin.close();
  } else {
   // can not handle the object
  }
 }
 out.close();
 return out.toByteArray();
}

代码示例来源:origin: mabe02/lanterna

@Override
public byte[] enquireTerminal(int timeout, TimeUnit timeoutTimeUnit) throws IOException {
  synchronized(terminalOutput) {
    terminalOutput.write(5);    //ENQ
    flush();
  }
  
  //Wait for input
  long startTime = System.currentTimeMillis();
  while(terminalInput.available() == 0) {
    if(System.currentTimeMillis() - startTime > timeoutTimeUnit.toMillis(timeout)) {
      return new byte[0];
    }
    try { 
      Thread.sleep(1); 
    } 
    catch(InterruptedException e) {
      return new byte[0];
    }
  }
  
  //We have at least one character, read as far as we can and return
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  while(terminalInput.available() > 0) {
    buffer.write(terminalInput.read());
  }
  return buffer.toByteArray();
}

代码示例来源:origin: apache/incubator-druid

@Override
public byte[] toBytes()
{
 try {
  final ByteArrayOutputStream out = new ByteArrayOutputStream();
  bitmap.serialize(new DataOutputStream(out));
  return out.toByteArray();
 }
 catch (Exception e) {
  throw Throwables.propagate(e);
 }
}

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

byte a[];
byte b[];

ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
outputStream.write( a );
outputStream.write( b );

byte c[] = outputStream.toByteArray( );

相关文章