Tableau d'octets Java à InputStream
1. Vue d'ensemble
Dans ce rapide didacticiel, nous allons illustrer comment utiliserconvert a simple byte[] to an InputStream, d'abord en utilisant java brut, puis la bibliothèque Guava.
Cet article fait partie dethe “Java – Back to Basic” series ici par exemple.
2. Convertir à l'aide de Java
Tout d'abord, examinonsthe Java solution:
@Test
public void givenUsingPlainJava_whenConvertingByteArrayToInputStream_thenCorrect()
throws IOException {
byte[] initialArray = { 0, 1, 2 };
InputStream targetStream = new ByteArrayInputStream(initialArray);
}
3. Convertir à l'aide de la goyave
Ensuite - utilisons envelopper le tableau d'octets dans le GuavaByteSource - ce qui nous permet ensuite deget the stream:
@Test
public void givenUsingGuava_whenConvertingByteArrayToInputStream_thenCorrect()
throws IOException {
byte[] initialArray = { 0, 1, 2 };
InputStream targetStream = ByteSource.wrap(initialArray).openStream();
}
Et là vous l'avez - un moyen simple d'ouvrir unInputStream à partir d'un tableau d'octets.