문제

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.

도움이 되었습니까?

해결책

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().

다른 팁

Socket sounds like what you are looking for.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top