0

I have to copy OutputStream to InputStream using StreamUtils.copy or something like that. I have the following code:

public void save(ByteArrayOutputStream outputStream) {
        InputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());  // I have to replace this code to StreamUtils.copy
        this.uploadObject(this.uuid.toString(), byteArrayInputStream);
    }

I have to replace this code InputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray()); to StreamUtils.copy or something like that.

John
  • 1,375
  • 4
  • 17
  • 40
  • You have described that incorrectly. You are trying to copy an input stream to an output stream. (It makes no sense to do it the other way. An input stream is a *source* not a *sink*.) – Stephen C Aug 12 '19 at 11:29
  • @StephenC I described all correctly because I get ```ByteArrayOutputStream``` as method parameter and in the last line of method ```save``` I have to call method ```this.uploadObject``` and this method need InputStream as parameter and I have to get it from ```ByteArrayOutputStream```. – John Aug 12 '19 at 11:34
  • @GovindaSakhare I saw this post but I couldn't found answer for my question in this post. – John Aug 12 '19 at 11:35
  • 1
    No. You described it incorrectly. You are **converting** a `ByteArrayOutputStream` into a `ByteArrayInputStream`. And note that this only works for the ByteArray streams because it is using BAOS / BAIS specific functionality. It doesn't work (or make sense) for streams in general as your title asks for. – Stephen C Aug 12 '19 at 11:46
  • @StephenC Oh yes you are right. Thanks for explain to me. – John Aug 12 '19 at 11:56
  • So what we have gotten past that .... it would seem that you can't replace that line with "StreamUtils.copy or something like that" because that's not what `StreamUtils.copy` does. What is the actual API for `uploadObject`? Does it really need an input stream? What is it doing? (Uploading to a remote, downloading from a remote, something else?) And what is wrong with your current solution? – Stephen C Aug 12 '19 at 12:00
  • @StephenC Its solution works fine but when I have a too much requests that I have too many copying bytes operations and I want find solution when I can get InputStream from Output stream without copy bytes operation. – John Aug 12 '19 at 12:08
  • Well it seems to me that the real problem is the design of the `uploadObject` method. Where does it come from? – Stephen C Aug 12 '19 at 12:28
  • @StephenC its just wrapper under the ```public PutObjectResult putObject( String bucketName, String key, InputStream input, ObjectMetadata metadata) throws SdkClientException, AmazonServiceException;``` method from amazon client. I get ```ByteArrayOutputStream``` and I have to write it to amazon service. – John Aug 12 '19 at 12:39
  • @GovindaSakhare you are was right. This post https://stackoverflow.com/questions/5778658/how-to-convert-outputstream-to-inputstream solved my issue. – John Aug 13 '19 at 07:53
  • This post https://stackoverflow.com/questions/5778658/how-to-convert-outputstream-to-inputstream solved my issue. – John Aug 13 '19 at 07:54
  • @Oleg Glad it helped :-) – Govinda Sakhare Aug 13 '19 at 08:47

0 Answers0