Reputation: 1399
I have some InputStream
as argument:
public byte[] ellipsis(InputStream is) {
// some code
}
I have some threshold, eg 10_000 bytes.
I want to do the following:
If InputStream
size is more than the threshold, it is necessary to cut 2/3 of the message from the beginning and 1000 bytes from the end from the incoming stream, and replace the middle with three dots (...), translate this into an array of bytes and return.
The whole problem is that I don't know the length of the stream until I read it.
Maybe there are ways to do this or some libraries for this?
Upvotes: 2
Views: 311
Reputation: 18255
You have to know about:
InputStream
is an unlimited stream.InputStream
contains available()
method, that retrieves the number of bytes available to ready (i.e. if you call it at the beginning, you can get the length of the stream)InputStream
support this method, and you can get 0
.So you have to read stream to the end to find out the length. This is the one way to do this with 100% probability.
Upvotes: 4