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

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

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

ByteArrayInputStream.<init>介绍

[英]Constructs a new ByteArrayInputStream on the byte array buf.
[中]在字节数组buf上构造新的ByteArrayInputStream。

代码示例

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

/**
 * Deserialize the byte array into an object.
 * @param bytes a serialized object
 * @return the result of deserializing the bytes
 */
@Nullable
public static Object deserialize(@Nullable byte[] bytes) {
  if (bytes == null) {
    return null;
  }
  try {
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
    return ois.readObject();
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Failed to deserialize object", ex);
  }
  catch (ClassNotFoundException ex) {
    throw new IllegalStateException("Failed to deserialize object type", ex);
  }
}

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

@SneakyThrows
private InputStream getInputStream(final Object value) {
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  objectOutputStream.writeObject(value);
  objectOutputStream.flush();
  objectOutputStream.close();
  return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}

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

@Test
public void whitespace() throws Exception {
  String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>";
  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(
      new ByteArrayInputStream(xml.getBytes("UTF-8")));
  SAXSource source = new SAXSource(staxXmlReader, new InputSource());
  DOMResult result = new DOMResult();
  transformer.transform(source, result);
  Node node1 = result.getNode().getFirstChild().getFirstChild();
  assertEquals(" ", node1.getTextContent());
  assertEquals(" Some text ", node1.getNextSibling().getTextContent());
}

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

@Test
public void test3() throws SQLException {
  SqlLobValue lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream("Bla".getBytes())), 12);
  thrown.expect(IllegalArgumentException.class);
  lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
}

代码示例来源: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: spring-projects/spring-framework

@Override
  protected RemoteInvocationResult doExecuteRequest(
      HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
    assertEquals("http://myurl", config.getServiceUrl());
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setContent(baos.toByteArray());
    exporter.handleRequest(request, response);
    return readRemoteInvocationResult(
        new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
  }
});

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

@SuppressWarnings("unchecked")
public static <X> X deserializeObject(byte[] bytes) throws IOException, ClassNotFoundException {
  try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new SerialVersionOverridingPythonObjectInputStream(bais)) {
    return (X) ois.readObject();
  }
}

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

ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
 in = new ObjectInputStream(bis);
 Object o = in.readObject(); 
 ...
} finally {
 try {
  if (in != null) {
   in.close();
  }
 } catch (IOException ex) {
  // ignore close exception
 }
}

代码示例来源: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: spring-projects/spring-framework

@Test
public void copyFromInputStream() throws IOException {
  byte[] content = "content".getBytes();
  ByteArrayInputStream in = new ByteArrayInputStream(content);
  ByteArrayOutputStream out = new ByteArrayOutputStream(content.length);
  int count = FileCopyUtils.copy(in, out);
  assertEquals(content.length, count);
  assertTrue(Arrays.equals(content, out.toByteArray()));
}

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

@Test
public void md5StringBuilder() throws IOException {
  String expected = "b10a8db164e0754105b7a99be72e3fe5";
  StringBuilder builder = new StringBuilder();
  DigestUtils.appendMd5DigestAsHex(bytes, builder);
  assertEquals("Invalid hash", expected, builder.toString());
  builder = new StringBuilder();
  DigestUtils.appendMd5DigestAsHex(new ByteArrayInputStream(bytes), builder);
  assertEquals("Invalid hash", expected, builder.toString());
}

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

@Test
public void unmarshalStreamSourceInputStream() throws Exception {
  StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
  Object flights = unmarshaller.unmarshal(source);
  testFlight(flights);
}

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

private void checkExceptionFromInvalidValueType(Throwable ex) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ex.printStackTrace(new PrintStream(baos));
  String dump = FileCopyUtils.copyToString(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
  assertTrue(dump.contains("someMessageSource"));
  assertTrue(dump.contains("useCodeAsDefaultMessage"));
}

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

@Override
  protected RemoteInvocationResult doExecuteRequest(
      HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
    assertEquals("http://myurl", config.getServiceUrl());
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setContent(baos.toByteArray());
    exporter.handleRequest(request, response);
    return readRemoteInvocationResult(
        new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
  }
});

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

public static KerberosTicket deserializeKerberosTicket(byte[] tgtBytes) {
  KerberosTicket ret;
  try {
    ByteArrayInputStream bin = new ByteArrayInputStream(tgtBytes);
    ObjectInputStream in = new ObjectInputStream(bin);
    ret = (KerberosTicket) in.readObject();
    in.close();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
  return ret;
}

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

@SuppressWarnings("deprecation") // testing a deprecated method
public void testWriteBytes_discardHighOrderBytes() throws IOException {
 /* Write out various test values in LITTLE ENDIAN FORMAT */
 out.writeBytes("\uAAAA\uAABB\uAACC");
 byte[] data = baos.toByteArray();
 /* Setup input streams */
 DataInput in = new DataInputStream(new ByteArrayInputStream(data));
 /* Read in various values NORMALLY */
 byte[] b = new byte[3];
 in.readFully(b);
 byte[] expected = {(byte) 0xAA, (byte) 0xBB, (byte) 0xCC};
 assertEquals(expected, b);
}

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

public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(buffer);
  out.writeObject(o);
  ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
  return in.readObject();
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testDeserializeStreamOfNull() throws Exception {
  final ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
  final ObjectOutputStream oos = new ObjectOutputStream(streamReal);
  oos.writeObject(null);
  oos.flush();
  oos.close();
  final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray());
  final Object test = SerializationUtils.deserialize(inTest);
  assertNull(test);
}

相关文章