From: Daniel Pitts on 3 Dec 2009 17:41 Anabolik wrote: > How to return to the begin of InputStream after reading this file? If it is a FileInputStream, you can use mark/reset, or you can re-open the file for reading. If you don't know what kind of InputStream it is, you can check if it markSupported(). If markSupported() is false, you can wrap the InputStream in a "BufferedInputStream", which does support mark/reset. Note that BufferedInputStream's mark() implementation will actually read the contents into memory, so you don't want to do that if you need to read a lot of large files concurrently. Again, if you know it is a file, you can use RandomAccessFile instead of InputStream. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: markspace on 3 Dec 2009 18:54 Arne Vajh�j wrote: > Anabolik wrote: >> How to return to the begin of InputStream after reading this file? > > Close and reopen the file. Good idea. Another in the same vein, if the stream is a network stream that can't be re-opened (easily), then make a temp file and copy it to the temp file. Then you can re-open the temp file as random access. File.createTempFile() I think is the name of the method to create a temporary file.
From: Roedy Green on 3 Dec 2009 20:08 On Wed, 2 Dec 2009 00:25:59 -0800 (PST), Anabolik <bumsys(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >How to return to the begin of InputStream after reading this file? Open another inputStream. e.g. // Read big-endian ( Java ) binary from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); DataInputStream dis = new DataInputStream( fis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[1024]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0 /* offset in ba */, ba.length /* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = (byte)dis.readUnsignedByte(); short us = (short)dis.readUnsignedShort(); // C L O S E dis.close(); // R E O P E N fis = new FileInputStream( "C:/temp/temp.in" ); dis = new DataInputStream( fis ); -- Roedy Green Canadian Mind Products http://mindprod.com I mean the word proof not in the sense of the lawyers, who set two half proofs equal to a whole one, but in the sense of a mathematician, where half proof = 0, and it is demanded for proof that every doubt becomes impossible. ~ Carl Friedrich Gauss
From: John B. Matthews on 3 Dec 2009 21:58 In article <e57da642-4748-4a6d-8988-15ae807819c3(a)r5g2000yqb.googlegroups.com>, Anabolik <bumsys(a)gmail.com> wrote: > How to return to the begin of InputStream after reading this file? Others have prudently suggested simply closing and re-opening the file, but I tried the mark/reset approach just to see how it worked: <http://sites.google.com/site/trashgod/edasm#DC> The object of the exercise was to meaningfully format certain portions of a legacy binary file. Using reset seemed a good way to explore, absent _a_priori_ knowledge of the correct relationships. The example uses "int value = in.read()" to advance through the data, but reading the entire stream and using "int value = data[index++]" would do as well. Still, I can see the value of being able to back-track while parsing a lengthy, non-file stream. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
From: Lothar Kimmeringer on 4 Dec 2009 05:23
markspace wrote: > Good idea. Another in the same vein, if the stream is a network stream > that can't be re-opened (easily), then make a temp file and copy it to > the temp file. Then you can re-open the temp file as random access. > > File.createTempFile() I think is the name of the method to create a > temporary file. It is but the file is not a temporary file that gets deleted automagically. You have to do that for yourself, otherwise your temporary folder get flooded by files over time. Regards, Lothar -- Lothar Kimmeringer E-Mail: spamfang(a)kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions! |