In java I'm having an inputstream
(maybe sequenceinputstream
) containing more than one file. I want to separate those files using java. Is there any solution available in java?
-
5How would you know where one stream ends and the next begins? – Jon Skeet Mar 14 '11 at 13:17
-
1What is the reason you can not open the files in separate streams? – Nishan Mar 14 '11 at 13:18
-
yes. i'm unable to identify start and end of the stream. or else shal i use some constant value in between the streams to differentiate the files. is it correct method to implement? – Roshan Mar 14 '11 at 13:25
-
1you seem to be reinventing the wheel. Maybe use an FTP library? FTP is one of a few protocols that defines how to send files over a stream. – Mark Peters Mar 14 '11 at 13:26
3 Answers
The simple answer is: you can't / shouldn't.
The complicated answer: in case you are dealing with a SequenceInputStream
, you can access the underlying streams using this reflection hack:
@SuppressWarnings("unchecked")
public static Enumeration<InputStream> split(final SequenceInputStream sis){
try{
Field streamsEnumField =
SequenceInputStream.class.getDeclaredField("e");
streamsEnumField.setAccessible(true);
return (Enumeration<InputStream>) streamsEnumField.get(sis);
} catch(final Exception e){
throw new IllegalStateException(e);
}
}
But I would be very careful about using something like this.

- 292,901
- 67
- 465
- 588
-
Also, at that point what are you gaining by using a SequenceInputStream? You know the set of InputStreams (since you're constructing the SIS with them) and you want to treat them individually so what's the point? – Mark Peters Mar 14 '11 at 13:29
-
@Mark the only use case I can think of is when the SequenceInputStream was created by code you can't control – Sean Patrick Floyd Mar 14 '11 at 13:30
-
-
@Sathish you can use it by pasting it into a java source file of your choice and calling the method. It's a reflection hack because it uses [reflection](http://download.oracle.com/javase/tutorial/reflect/index.html) to access internal state that's supposed to be hidden from access. This is evil, but sometimes evil things need to be done. – Sean Patrick Floyd Mar 14 '11 at 13:48
-
using httpclientrequest servlet i'm sending the sequenceinputstream of some files. While in server side i'm receiving inputstream as servletinputstream. Please tel me the ways to convert servletinputstream to sequenceinputstream. Thanks in advance – Roshan Mar 15 '11 at 07:06
This depends on what kind of files you have, if there is any reserved squence which indicates the End and/or the beginning of the file like HTML or XML etc. then you can separate them, otherwise i don't see any way to do this.

- 36,908
- 70
- 97
- 130
-
-
Guess it would be
or any xml-End_Tag, am i wrong? So by saving the information localy and parsing it this should be possible.
– CloudyMarble Mar 14 '11 at 13:40 -
-
: That may often be the case, but really the root-ending node just represents the end of a certain data structure. There could at the very least be whitespace or comments after. – Mark Peters Mar 14 '11 at 13:43
-
@Mark: AFAIK in a valid html/xml fiel there should be no data after the closing root element, anyway html and xml was just an Example of data which has Enddata sequence, its up to the OP to decide what kind of Data he is receiving and if the data really valid or no. – CloudyMarble Mar 14 '11 at 13:51
You need to have some way of determining where one file ends and the next begins- most files reserve the first few bytes as an "identity" tag. Bear in mind that Java's insistence on having a SIGNED byte class can mess with identifying these at runtime.
However, you can't really split streams. So, you'll have to stage your data through ordinary byte[] arrays. I'd read from the parent stream in chunks of 4K (for files) or 64K (for the network) since the underlying native calls usually use those sizes, store the chunks in lists, and then create a ByteArrayInputStream for each file at the end.

- 201
- 3
- 2