Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

InputStream read Buffer problem

807591Mar 26 2008 — edited Mar 26 2008
Hi, Im now developing a function reading an online .doc file and save it in a tem local directory. Here is a piece of my code:
OutputStream out = new FileOutputStream(
				"C:\\Documents and Settings\\xosun\\My Documents\\temDocFile");
		Properties systemSettings = System.getProperties();
		systemSettings.put("http.proxyHost", "129.60.173.53");
		systemSettings.put("http.proxyPort", "20066");

		URL u = new URL(onlineDocURL);

		try {
                        InputStream in = u.openStream();
			
			byte[] buf = new byte[1000 * 1024]; 
			int bytesRead;
			while ((bytesRead = in.read(buf)) > 0) {   //Here is what the problem happens!
				out.write(buf, 0, bytesRead);
				out.flush();
			}
		} catch (java.lang.IllegalArgumentException e) {
			System.out.println("java.net.URL.openStream(Unknown Source): "
					+ e.toString());
			//to be implemented later

		} catch (java.io.IOException e2) {
			System.out.println("Problem reading  file " + e2.getMessage());
			//to be implemented later
		}

		out.close();
My problem is about the line : while ((bytesRead = in.read(buf)) > 0)
in.read(buf) takes too long time (And I suspect the buffer cannot be read) and the whole program just suspended there without moving forward. I would like to know if there is any method that I can break the while loop if in.read(buf) takes more than 5 seconds? Thanks in advance!!

Best, SHannon
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2008
Added on Mar 26 2008
2 comments
439 views