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

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

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

ByteArrayInputStream介绍

[英]A specialized InputStream for reading the contents of a byte array.
[中]用于读取字节数组内容的专用输入流。

代码示例

代码示例来源: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 deserializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
  ByteArrayInputStream in = new ByteArrayInputStream((byte[]) storeValue);
  try {
    return serialization.deserialize(in);
  }
  finally {
    in.close();
  }
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

@Override
public int read(byte[] buffer, long offset, int length) throws ProxyCacheException {
  if (offset >= data.length) {
    return -1;
  }
  if (offset > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("Too long offset for memory cache " + offset);
  }
  return new ByteArrayInputStream(data).read(buffer, (int) offset, length);
}

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

private static void skipCondition(DataInputView in) throws IOException, ClassNotFoundException {
  boolean hasCondition = in.readBoolean();
  if (hasCondition) {
    int length = in.readInt();
    byte[] serCondition = new byte[length];
    in.read(serCondition);
    ByteArrayInputStream bais = new ByteArrayInputStream(serCondition);
    ObjectInputStream ois = new ObjectInputStream(bais);
    ois.readObject();
    ois.close();
    bais.close();
  }
}

代码示例来源:origin: lets-blade/blade

public StaticInputStream(InputStream input) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byteBuf = Unpooled.buffer();
  // Fake code simulating the copy
  // You can generally do better with nio if you need...
  // And please, unlike me, do something about the Exceptions :D
  byte[] buffer = new byte[1024];
  int    len;
  while ((len = input.read(buffer)) > -1) {
    size += len;
    baos.write(buffer, 0, len);
  }
  baos.flush();
  // Open new InputStreams using the recorded bytes
  // Can be repeated as many times as you wish
  this.inputStream = new ByteArrayInputStream(baos.toByteArray());
  byteBuf.writeBytes(this.inputStream, size);
}

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

@Override
public KafkaConfig clone() {
  try {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SERIALIZER.serialize(this, new DataOutputStream(baos));
    return SERIALIZER.deserialize(new DataInputStream(new ByteArrayInputStream(baos.toByteArray())));
  } catch (IOException e) {
    throw new RuntimeException(e);//in mem, should not happen
  }
}

代码示例来源:origin: OfficeDev/ews-java-api

ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
  memoryStream.writeTo(urlOutStream);
  urlOutStream.flush();
  urlOutStream.close();
  memoryStream.close();
 } else {
  PrintWriter writer = new PrintWriter(urlOutStream);
  urlOutStream.flush();
  urlOutStream.close();
   int data = serviceResponseStream.read();
   if (-1 == data) {
    break;
  ByteArrayInputStream memoryStreamIn = new ByteArrayInputStream(
    memoryStream.toByteArray());
  EwsXmlReader reader = new EwsXmlReader(memoryStreamIn);
 serviceResponseStream.close();
} finally {
 if (request != null) {

代码示例来源:origin: cSploit/android

if((read = mReader.read(buffer, 0, MAX_REQUEST_SIZE)) > 0){
 Profiler.instance().profile("proxy request parse");
 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer, 0, read);
 BufferedReader bReader = new BufferedReader(new InputStreamReader(byteArrayInputStream));
 StringBuilder builder = new StringBuilder();
   mServerWriter.write(request.getBytes());
   mServerWriter.flush();
 mReader.close();

代码示例来源:origin: haraldk/TwelveMonkeys

private void runStreamTest(int pLength) throws Exception {
  byte[] data = createData(pLength);
  ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
  OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true);
  out.write(data);
  out.close();
  byte[] encoded = outBytes.toByteArray();
  byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()));
  assertArrayEquals(String.format("Data %d", pLength), data, decoded);
  InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder());
  outBytes = new ByteArrayOutputStream();
  FileUtil.copy(in, outBytes);
  outBytes.close();
  in.close();
  decoded = outBytes.toByteArray();
  assertArrayEquals(String.format("Data %d", pLength), data, decoded);
}

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

enum StringCompressor {
  ;
  public static byte[] compress(String text) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      OutputStream out = new DeflaterOutputStream(baos);
      out.write(text.getBytes("UTF-8"));
      out.close();
    } catch (IOException e) {
      throw new AssertionError(e);
    }
    return baos.toByteArray();
  }

  public static String decompress(byte[] bytes) {
    InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      byte[] buffer = new byte[8192];
      int len;
      while((len = in.read(buffer))>0)
        baos.write(buffer, 0, len);
      return new String(baos.toByteArray(), "UTF-8");
    } catch (IOException e) {
      throw new AssertionError(e);
    }
  }
}

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

DataInputStream diffIn = new DataInputStream(new ByteArrayInputStream(diffBuf, 0, diffSize));
diffIn.close();
InputStream in = new ByteArrayInputStream(diffBuf, 0, diffSize);
in.skip(BSUtil.HEADER_SIZE);
DataInputStream ctrlBlockIn = new DataInputStream(new GZIPInputStream(in));
in = new ByteArrayInputStream(diffBuf, 0, diffSize);
in.skip(ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream diffBlockIn = new GZIPInputStream(in);
in = new ByteArrayInputStream(diffBuf, 0, diffSize);
in.skip(diffBlockLen + ctrlBlockLen + BSUtil.HEADER_SIZE);
InputStream extraBlockIn = new GZIPInputStream(in);
      outStream.close();
      return RETURN_DIFF_FILE_ERR;
      outStream.close();
      return RETURN_DIFF_FILE_ERR;
      outStream.close();
      return RETURN_DIFF_FILE_ERR;

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

public static final void testSerialization(String[] values) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
  DataOutputStream serializer = new DataOutputStream(baos);
  
  for (String value : values) {
    StringValue.writeString(value, serializer);
  }
  
  serializer.close();
  baos.close();
  
  ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  DataInputStream deserializer = new DataInputStream(bais);
  
  int num = 0;
  while (deserializer.available() > 0) {
    String deser = StringValue.readString(deserializer);
    
    assertEquals("DeserializedString differs from original string.", values[num], deser);
    num++;
  }
  
  assertEquals("Wrong number of deserialized values", values.length, num);
}

代码示例来源: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: org.netbeans.api/org-openide-filesystems

private InputStream getMemInputStream(JarFile jf, JarEntry je)
throws IOException {
  InputStream is = getInputStream4336753(jf, je);
  ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());
  try {
    FileUtil.copy(is, os);
  } finally {
    os.close();
  }
  return new ByteArrayInputStream(os.toByteArray());
}

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

@Override
@Before
public void setup() throws IOException {
  original = new MoreComplexObject();
  final ByteArrayOutputStream bos = willClose(new ByteArrayOutputStream());
  final ObjectOutputStream oos = willClose(new ObjectOutputStream(bos));
  oos.writeObject(original);
  inputStream = willClose(new ByteArrayInputStream(bos.toByteArray()));
}

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

public void testSkipBytes() throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream out = new DataOutputStream(baos);

  /* Write out various test values NORMALLY */
  out.write(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); // 10 bytes of junk to skip
  initializeData(out);

  byte[] data = baos.toByteArray();

  DataInput in = new LittleEndianDataInputStream(new ByteArrayInputStream(data));
  int bytesSkipped = 0;
  while (bytesSkipped < 10) {
   bytesSkipped += in.skipBytes(10 - bytesSkipped);
  }

  /* Read in various values in LITTLE ENDIAN FORMAT */
  byte[] b = new byte[2];
  in.readFully(b);
  assertEquals(-100, b[0]);
  assertEquals(100, b[1]);
  assertTrue(in.readBoolean());
  assertFalse(in.readBoolean());
 }
}

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

@Test
public void copyToString() throws Exception {
  Charset charset = Charset.defaultCharset();
  InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset)));
  String actual = StreamUtils.copyToString(inputStream, charset);
  assertThat(actual, equalTo(string));
  verify(inputStream, never()).close();
}

代码示例来源:origin: org.apache.portals.pluto/pluto-util

public void writeTo(OutputStream out, int buflen) throws IOException {
  byte[] buf = new byte[buflen];
  int read = 0;
  InputStream sinkReader = new BufferedInputStream( 
      new ByteArrayInputStream( ((ByteArrayOutputStream)this.out).toByteArray()), buflen);
  while ( ( read = sinkReader.read(buf) ) != -1 ) {
    out.write(buf, 0, read);
  }
}

相关文章