This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch to file/Connection.java
- From: Per Bothner <per at bothner dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 20 Feb 2002 11:34:38 -0800
- Subject: Patch to file/Connection.java
This brings opening of file URLs closer to JDK behavior.
The immediate problem was that the FileNotFoundException
did not include the filename in the message. I noticed
from a JDK exception stack trace that JDK was opening
the InputStream in connect. The suggested that getInputStream
should not open a stream, but return an already-opened one.
A test program verified that call getInputStream twice
would return the identical InputStream each time.
As a side-effect we get more consistent FileNotFoundExceptions.
I will check this in unless someone complains loadly and quickly enough.
--
--Per Bothner
per@bothner.com http://www.bothner.com/per/
2002-02-20 Per Bothner <per@bothner.com>
* gnu/gcj/protocol/file/Connection.java (conect): Open the input
and/or output streams immediately here, instead of using File.exists.
(inputStream, outputStream): New fields to save open streams.
(getInputStream, getOutputStream): Use already-opened streams.
Index: libjava/gnu/gcj/protocol/file/Connection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/gcj/protocol/file/Connection.java,v
retrieving revision 1.4
diff -u -r1.4 Connection.java
--- Connection.java 2001/09/30 07:52:16 1.4
+++ Connection.java 2002/02/20 19:27:16
@@ -33,6 +33,8 @@
private Vector hdrVec = new Vector();
private boolean gotHeaders = false;
private File fileIn;
+ private InputStream inputStream;
+ private OutputStream outputStream;
public Connection(URL url)
{
@@ -47,34 +49,36 @@
return;
// If not connected, then file needs to be openned.
- fileIn = new File(url.getFile());
-
- if (fileIn.exists())
- connected = true;
- else
- throw new FileNotFoundException("No such file or directory");
+ String fname = url.getFile();
+ fileIn = new File(fname);
+ if (doInput)
+ inputStream = new BufferedInputStream(new FileInputStream(fileIn));
+ if (doOutput)
+ outputStream = new BufferedOutputStream(new FileOutputStream(fileIn));
+ connected = true;
}
public InputStream getInputStream() throws IOException
{
+ if (! doInput)
+ throw new ProtocolException("Can't open InputStream if doInput is false");
if (!connected)
connect();
- if (! doInput)
- throw new ProtocolException("Can't open InputStream if doInput is false");
- return new BufferedInputStream(new FileInputStream(fileIn));
+ return inputStream;
}
// Override default method in URLConnection.
public OutputStream getOutputStream() throws IOException
{
- if (!connected)
- connect();
-
if (! doOutput)
throw new
ProtocolException("Can't open OutputStream if doOutput is false");
- return new BufferedOutputStream(new FileOutputStream(fileIn));
+
+ if (!connected)
+ connect();
+
+ return outputStream;
}
// Override default method in URLConnection.