Question

I am receiving files through a socket and saving them to database.

So, i'm receiving the byte stream, and passing it to a back-end process, say Process1 for the DB save.

I'm looking to do this without saving the stream on disk. So, rather than storing the incoming stream as a file on disk and then passing that file to Process1, i'm looking to pass it while it's still in the memory. This is to eliminate the time-costly disk read & write.

One way i can do is to pass the byte[] to Process1. I'm wondering whether there's a better way of doing this.

TIA.

Was it helpful?

Solution

You can use a ByteArrayOutputStream. It is, essentially, a growable byte[] which you can write into at will, that is in the limit of your available heap space.

After having written to it/flushed it/closed it (although those two last operations are essentially a no-op, that's no reason for ditching sane practices), you can obtain the underlying byte array using this class's .toByteArray().

OTHER TIPS

Socket sounds like what you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top