0

In one of the APIs, S3Object is converted to String using Apache IOUtils using the below steps:

S3ObjectInputStream inputStream = s3Object.getObjectContent();
String streamString = IOUtils.toString(inputStream);

In the other API, when I have to convert the string back to S3ObjectInputStream, I try the following steps, but it doesn't seem to work:

Approach 1:

String streamString = IOUtils.toString(inputStream); // --> from the 1st API

S3ObjectInputStream s3ObjectInputStream = new S3ObjectInputStream
        (new ByteArrayInputStream(streamString.getBytes()), null); --> Returning an incorrect/null inputstream

Approach 2:

String streamString = IOUtils.toString(inputStream); // --> from the 1st API

InputStream sampleStream = IOUtils.toInputStream(streamString, StandardCharsets.UTF_8);

Is there any other way to convert it correctly without losing data?

Angel C
  • 29
  • 10
  • 1
    Does this answer your question? [How do I convert an InputStream to S3ObjectInputStream in Java?](https://stackoverflow.com/questions/27444766/how-do-i-convert-an-inputstream-to-s3objectinputstream-in-java) – hfontanez Jul 17 '22 at 15:48

1 Answers1

0

I tried and it works alright. Your issue probably lies somewhere else (probably your inputStream).

Here is the working code (with your code included).

@Test
  public void testS3ObjectInputStream() throws IOException {

    String originalString = "Hello World";
    InputStream inputStream =
        new ByteArrayInputStream(originalString.getBytes(StandardCharsets.UTF_8));
   
     // your code
    String streamString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    S3ObjectInputStream s3ObjectInputStream =
        new S3ObjectInputStream(
            new ByteArrayInputStream(streamString.getBytes(StandardCharsets.UTF_8)), null);

    assertNotNull(s3ObjectInputStream); //PASSES!

    String recoveredString = IOUtils.toString(s3ObjectInputStream, StandardCharsets.UTF_8);

    assertEquals(originalString, recoveredString); //PASSES!
  }
Tintin
  • 2,853
  • 6
  • 42
  • 74