java - How to use inputstream mark and reset functions? -
i'm trying read first 8192
bytes file , run bytes through method returns boolean.
that boolean tells me if file of particular type.
if method returns true on bytes file type want remaining bytes , run them through different method. if false, run remaining bytes through different method.
i'm trying use mark, having no success.
private final void handlefile(inputstream inputstream) { bufferedinputstream bis = new bufferedinputstream(inputstream); bis.mark(8192); byte[] startingbytes = inputstreamtobytearray(bis); if(startingbytes.length == 0) { return; } byte[] finalbytes; if(isfiletype(startingbytes)) { bis.reset(); finalbytes = inputstreamtobytearray(bis); methodforfinalbytes(finalbytes); } else { // other stuff; } } private byte[] inputstreamtobytearray(inputstream inputstream) { bytearrayoutputstream baos = new bytearrayoutputstream(); byte[] buffer = new byte[8192]; try { while(inputstream.read(buffer) != -1) { baos.write(buffer); } } catch(ioexception ioe) { ioe.printstacktrace(); } return baos.tobytearray(); }
problem picking left off while keeping byte array in chunks (for processing against large files). also, i'm getting 8192
bytes returned , not remaining.
any suggestions?
first, looks inputstreamtobytearray()
is reading entire stream until end of file, not first 8192 bytes. want read first 8192 bytes separately first.
second, want re-read bytes again? if not, i'm not sure if need mark/reset. (especially since still have byte array )
the code below reads first 8192 bytes first, decides do:
byte[] header = new byte[8192]; //reads entire array or until eof whichever first bis.read(header); if(isfiletype(header)) { byte[] finalbytes = inputstreamtobytearray(bis); methodforfinalbytes(finalbytes); } else { // other stuff; }
Comments
Post a Comment