Results 1 to 7 of 7
  1. #1
    davidjava is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default How can I repeatedly read content from the BufferedInputStream?

    Merry Christmas!!!

    I was trying to read the content of BufferedInputStream repeatedly. Each time when I call the read() method, it changed the mark/pos/count. I tried to reset(), or even created a subclass of BufferedInputStream.java, and added some methods to reset these values. It still could do I wanted to.

    Here is what my code trying to do.

    Step 1) Read the header of the stream
    Step 2) Read all content of the stream
    Step 3) Read the header again (for whatever reason)

    Step 1 works fine, as designed.
    Step 2 reads from where Step1 ends
    Step 3 reads nothing 'cause Step 2 already has reached to the end of file.

    How can I make Step 2 & 3 work correctly, using the the same object of BufferedInputStream?


    Thank you in advance,

    David


    Java Code:
    public class BufferedInputStreamTester {
    	
    	private int fileLength = 598669;
    	private int headLength = 50; 
    	
    	
    	/**
    	 * This method reads contents of a file and print it out
    	 * @throws FileNotFoundException 
    	 */
    	public void readFromFile(String filename) throws FileNotFoundException {
    
    		BufferedInputStream bufferedInput = null;
    		byte[] buffer = new byte[fileLength];
    
    		FileInputStream fis = new FileInputStream(filename);
    		try {				
    			// Construct the BufferedInputStream object
    			bufferedInput = new BufferedInputStream(fis);
    
    			// 1st cal to BufferedInputStream
    			byte[] header = getFileheader(bufferedInput);
    			System.out.print("header::\n" + new String(header));
    			
    			//2nd cal to BufferedInputStream
    			int bytesRead = 0;
    
    			// Keep reading from the file while there is any content
    			// when the end of the stream has been reached, -1 is returned
    			while ((bytesRead = bufferedInput.read(buffer)) != -1) {
    
    				// Process the chunk of bytes read
    				// in this case we just construct a String and print it out
    				String chunk = new String(buffer, 0, bytesRead);
    				System.out.print("\n\n\nchunk::\n" + chunk);
    			}
    			
    			 //3rd cal to BufferedInputStream			 
    			bytesRead = 0;
    			byte[] head2 = new byte[headLength];
    			
    			while (bytesRead < headLength) {
    				int n = bufferedInput.read(head2, bytesRead, head2.length - bytesRead);
    				if (n <= 0)
    					break;
    				bytesRead += n;
    			}					
    			System.out.print("\n\n\nhead2::\n" + new String(head2));
    		
    			
    		} catch (FileNotFoundException ex) {
    			ex.printStackTrace();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} finally {
    			// Close the BufferedInputStream
    			try {
    				if (bufferedInput != null)
    					bufferedInput.close();
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			}
    		}
    
    	}
    
    	private byte[] getFileheader(BufferedInputStream bufferedInput)
    			throws IOException {
    		byte[] header = new byte[headLength];
    		int bytes_read = 0;
    		while (bytes_read < header.length) {
    			int n = bufferedInput.read(header, bytes_read, header.length - bytes_read);
    			if (n <= 0)
    				break;
    			bytes_read += n;
    		}
    		if (bytes_read < header.length) {
    			if (bufferedInput != null) {
    				bufferedInput.close();
    			}
    			throw new IOException(" prescan partial file -- bytes read: "
    					+ bytes_read + " file Length: " + header.length);
    		}
    		
    		return header;
    	}
    	
    	
    	public static void main(String[] args) {
    		try {
    			//TODO: you'll need to change the test file path
    			new BufferedInputStreamTester().readFromFile("C:\\tstWorkSpace\\dummy\\resources\\8415728.wav");
    		} catch (FileNotFoundException e) {		
    			e.printStackTrace();
    		}
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    Post "approved". Welcome to Java-Forums.org!

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    I tried to reset(), or even created a subclass of BufferedInputStream.java, and added some methods to reset these values. It still could do I wanted to.
    How did you use mark()/reset()? What happened when you did that?

    And why don't you just use those 50 bytes as read the first time?
    Last edited by pbrockway2; 12-24-2011 at 01:11 AM.

  4. #4
    davidjava is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    Thanks for the quick response.

    And why don't you just use those 50 bytes as read the first time?
    Yes, I can.

    That's just the extreme case I want demo that I didn't read anything at Step 3.


    How did you use mark()/reset()? What happened when you did that?
    I was a little confused about mark(), so I didn't use it. However, when I applied reset() in

    Java Code:
    			//2nd cal to BufferedInputStream
    			int bytesRead = 0;
    
    			// Keep reading from the file while there is any content
    			// when the end of the stream has been reached, -1 is returned
    			while ((bytesRead = bufferedInput.read(buffer)) != -1) {
    
    				// Process the chunk of bytes read
    				// in this case we just construct a String and print it out
    				String chunk = new String(buffer, 0, bytesRead);
    				System.out.print("\n\n\nchunk::\n" + chunk);
    			}
    			
    			bufferedInput.reset();
    			
    			 //3rd cal to BufferedInputStream			 
    			bytesRead = 0;
    			byte[] head2 = new byte[headLength];
    			
    			while (bytesRead < headLength) {
    				int n = bufferedInput.read(head2, bytesRead, head2.length - bytesRead);
    				if (n <= 0)
    					break;
    				bytesRead += n;
    			}					
    			System.out.print("\n\n\nhead2::\n" + new String(head2));

    I got this error.

    java.io.IOException: Resetting to invalid mark
    at java.io.BufferedInputStream.reset(BufferedInputStr eam.java:408)
    at com.liao.is.BufferedInputStreamTester.readFromFile (BufferedInputStreamTester.java:46)
    at com.liao.is.BufferedInputStreamTester.main(Buffere dInputStreamTester.java:102)


    If I use mark() and reset() together, as

    bufferedInput.mark(fileLength); // bufferedInput.mark(0);
    bufferedInput.reset();

    The error went away. But I still got nothing in "head2".

    Guess I don't figure out how to use mark()/reset(), could you please shed me some light?


    Thanks a lot,

    David

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    OK, I had never used mark/reset but I used your code - minus printing the chunks, I just printed their length - consulted the API docs and, yes, you have to call mark() to establish where reset() will go back to. But the thing is you have to mark() *before* you read the header fhe first time, that way the reset will go all the way back to the start.

    Try that and see what happens. If you have problems post back with the updated code so we can be sure we're talking about the same thing.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    Quote Originally Posted by davidjava View Post
    bufferedInput.mark(fileLength); // bufferedInput.mark(0);
    If the file happens to be large, you'll end up with a very large input buffer; it works, but realize that this happens, i.e. you can read as far ahead as the size of the input buffer; the mark() call marks a position in that buffer so that the reset() call can reposition the current position in the input stream.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    davidjava is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: How can I repeatedly read content from the BufferedInputStream?

    Hi All,

    Thanks for your responses and hints. I got this problem fixed.

    Here is the code that fulfills my original purposes of reading the Input Stream multiple times.

    Java Code:
    public class BufferedInputStreamTester {
    	
    	private int fileLength = 598669;
    	private int headLength = 50; 
    	
    	
    	/**
    	 * This method reads contents of a file and print it out
    	 * @throws FileNotFoundException 
    	 */
    	public void readFromFile(String filename) throws FileNotFoundException {
    
    		BufferedInputStream bufferedInput = null;
    		byte[] buffer = new byte[fileLength];
    
    		FileInputStream fis = new FileInputStream(filename);
    		try {				
    			// Construct the BufferedInputStream object
    			bufferedInput = new BufferedInputStream(fis);
    
    			// 1st cal to BufferedInputStream
    			if(bufferedInput.markSupported()){
    				bufferedInput.mark(headLength);
    				byte[] header = getFileheader(bufferedInput);
    				System.out.print("header::\n" + new String(header));
    			}
    			bufferedInput.reset();
    			
    			//2nd cal to BufferedInputStream			
    			if(bufferedInput.markSupported()){
    				bufferedInput.mark(fileLength);
    				buffer = getAllFileContent(bufferedInput);
    			}			
    			bufferedInput.reset();
    			String chunk = new String(buffer, 0, fileLength);
    			System.out.print("\n\n\nchunk::\n" + chunk);
    			
    			 //3rd cal to BufferedInputStream			 
    			int bytesRead = 0;
    			byte[] head2 = new byte[headLength];
    			
    			while (bytesRead < headLength) {
    				int n = bufferedInput.read(head2, bytesRead, head2.length - bytesRead);
    				if (n <= 0)
    					break;
    				bytesRead += n;
    			}					
    			System.out.print("\n\n\nhead2::\n" + new String(head2));
    		
    			
    		} catch (FileNotFoundException ex) {
    			ex.printStackTrace();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} finally {
    			// Close the BufferedInputStream
    			try {
    				if (bufferedInput != null)
    					bufferedInput.close();
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			}
    		}
    
    	}
    
    	private byte[] getFileheader(BufferedInputStream bufferedInput)
    			throws IOException {
    		byte[] header = new byte[headLength];
    		int bytes_read = 0;
    		while (bytes_read < header.length) {
    			int n = bufferedInput.read(header, bytes_read, header.length - bytes_read);
    			if (n <= 0)
    				break;
    			bytes_read += n;
    		}
    		if (bytes_read < header.length) {
    			if (bufferedInput != null) {
    				bufferedInput.close();
    			}
    			throw new IOException(" prescan partial file -- bytes read: "
    					+ bytes_read + " file Length: " + header.length);
    		}
    		
    		return header;
    	}
    	
    	private byte[] getAllFileContent(BufferedInputStream bufferedInput)	throws IOException {
    		byte[] content = new byte[fileLength];
    		int bytes_read = 0;
    		while (bytes_read < content.length) {
    			int n = bufferedInput.read(content, bytes_read, content.length - bytes_read);
    			if (n <= 0)
    				break;
    			bytes_read += n;
    		}
    		if (bytes_read < content.length) {
    			if (bufferedInput != null) {
    				bufferedInput.close();
    			}
    			throw new IOException(" getAllFileContent() :: prescan partial file -- bytes read: "
    					+ bytes_read + " file Length: " + content.length);
    		}
    
    		return content;
    	}
    	public static void main(String[] args) {
    		try {
    			//TODO: you'll need to change the test file path
    			new BufferedInputStreamTester().readFromFile("C:\\tstWorkSpace\\dummy\\resources\\8415728.wav");
    		} catch (FileNotFoundException e) {		
    			e.printStackTrace();
    		}
    	}
    
    }

Similar Threads

  1. How to read content from a file
    By eden83 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 10:25 PM
  2. Read PDF Content
    By Deepa in forum New To Java
    Replies: 3
    Last Post: 11-03-2009, 10:55 AM
  3. Read text content from pdf using pdfbox
    By cssatheesh in forum Advanced Java
    Replies: 0
    Last Post: 09-22-2009, 08:54 PM
  4. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 01:03 PM
  5. Read the content of file from a specified path
    By shiva in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 07:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •