Index: ChangeLog =================================================================== RCS file: /cvsroot/classpath/classpath/ChangeLog,v retrieving revision 1.1720 diff -u -b -B -r1.1720 ChangeLog --- ChangeLog 27 Dec 2003 10:48:48 -0000 1.1720 +++ ChangeLog 27 Dec 2003 12:39:12 -0000 @@ -1,3 +1,17 @@ +2003-12-27 Michael Koch
+ + * gnu/java/net/protocol/http/Connection.java + (outputWriter): Removed. + (connect): Always initialize inputStream, don't initialize + outputWriter. + (sendRequest): Create PrintWriter object locally. + (receiveReply): Made private. + (getInputStream): Return inputStream confitionally. + (getOuputStream): Documentation added. + (setRequestMethod): Reordered. + (getHeaderField): Reordered. + (getHeaderFieldKey): Reordered. + 2003-12-27 Jeroen Frijters * java/lang/System.java (mapLibraryName): Changed to call Index: gnu/java/net/protocol/http/Connection.java =================================================================== RCS file: /cvsroot/classpath/classpath/gnu/java/net/protocol/http/Connection.java,v retrieving revision 1.6 diff -u -b -B -r1.6 Connection.java --- gnu/java/net/protocol/http/Connection.java 2 Dec 2003 09:28:22 -0000 1.6 +++ gnu/java/net/protocol/http/Connection.java 27 Dec 2003 12:39:12 -0000 @@ -119,11 +119,6 @@ private ByteArrayOutputStream bufferedOutputStream; /** - * The PrintWriter for this connection (used internally) - */ - private PrintWriter outputWriter; - - /** * This is the object that holds the header field information */ private HeaderFieldHelper headers = new HeaderFieldHelper(); @@ -164,13 +159,11 @@ socket = new Socket(url.getHost(), port); } - if (doInput) - inputStream = new DataInputStream - (new BufferedInputStream (socket.getInputStream())); + inputStream = + new DataInputStream(new BufferedInputStream(socket.getInputStream())); outputStream = new BufferedOutputStream (socket.getOutputStream()); bufferedOutputStream = new ByteArrayOutputStream (256); //default is too small - outputWriter = new PrintWriter (new OutputStreamWriter (outputStream, "8859_1")); sendRequest(); receiveReply(); @@ -202,36 +195,30 @@ */ void sendRequest() throws IOException { + // Create PrintWriter for easier sending of headers. + PrintWriter outputWriter = + new PrintWriter(new OutputStreamWriter(outputStream, "8859_1")); + // Send request including any request properties that were set. outputWriter.print (getRequestMethod() + " " + url.getFile() + " HTTP/1.1\r\n"); // Set additional HTTP headers. if (getRequestProperty ("Host") == null) - { setRequestProperty ("Host", url.getHost()); - } if (getRequestProperty ("Connection") == null) - { setRequestProperty ("Connection", "Close"); - } if (getRequestProperty ("user-agent") == null) - { setRequestProperty ("user-agent", "gnu-classpath/" + System.getProperty ("classpath.version")); - } if (getRequestProperty ("accept") == null) - { setRequestProperty ("accept", "*/*"); - } if (getRequestProperty ("Content-type") == null) - { setRequestProperty ("Content-type", "application/x-www-form-urlencoded"); - } // Set correct content length. setRequestProperty ("Content-length", String.valueOf (bufferedOutputStream.size())); @@ -257,7 +244,7 @@ /** * Read HTTP reply from inputStream. */ - void receiveReply() throws IOException + private void receiveReply() throws IOException { // Parse the reply String line = inputStream.readLine(); @@ -349,27 +336,6 @@ } /** - * Overrides java.net.HttpURLConnection.setRequestMethod() in order to - * restrict the available methods to only those we support. - * - * @param method The RequestMethod to use - * - * @exception ProtocolException If the specified method is not valid - */ - public void setRequestMethod (String method) throws ProtocolException - { - method = method.toUpperCase(); - - if (method.equals("GET") - || method.equals("HEAD") - || method.equals("POST")) - super.setRequestMethod (method); - else - throw new ProtocolException ("Unsupported or unknown request method " + - method); - } - - /** * Return a boolean indicating whether or not this connection is * going through a proxy * @@ -381,34 +347,6 @@ } /** - * This method returns the header field key at the specified numeric - * index. - * - * @param n The index into the header field array - * - * @return The name of the header field key, ornull
if the
- * specified index is not valid.
- */
- public String getHeaderFieldKey (int n)
- {
- return headers.getHeaderFieldKeyByIndex (n);
- }
-
- /**
- * This method returns the header field value at the specified numeric
- * index.
- *
- * @param n The index into the header field array
- *
- * @return The value of the specified header field, or null
- * if the specified index is not valid.
- */
- public String getHeaderField (int n)
- {
- return headers.getHeaderFieldValueByIndex (n);
- }
-
- /**
* Returns an InputStream for reading from this connection. This stream
* will be "queued up" for reading just the contents of the requested file.
* Overrides URLConnection.getInputStream()
@@ -419,15 +357,22 @@
*/
public InputStream getInputStream() throws IOException
{
- if(inputStream != null)
- return inputStream;
-
if (!connected)
connect();
+ if (!doInput)
+ throw new ProtocolException("Can't open InputStream if doInput is false");
+
return inputStream;
}
+ /**
+ * Returns on OutputStream for writing to this connection.
+ *
+ * @return An OutputStream for this connection.
+ *
+ * @exception IOException If an error occurs
+ */
public OutputStream getOutputStream() throws IOException
{
if (!doOutput)
@@ -443,5 +388,52 @@
return bufferedOutputStream;
}
+ /**
+ * Overrides java.net.HttpURLConnection.setRequestMethod() in order to
+ * restrict the available methods to only those we support.
+ *
+ * @param method The RequestMethod to use
+ *
+ * @exception ProtocolException If the specified method is not valid
+ */
+ public void setRequestMethod (String method) throws ProtocolException
+ {
+ method = method.toUpperCase();
+ if (method.equals("GET")
+ || method.equals("HEAD")
+ || method.equals("POST"))
+ super.setRequestMethod (method);
+ else
+ throw new ProtocolException ("Unsupported or unknown request method " +
+ method);
+ }
+
+ /**
+ * This method returns the header field value at the specified numeric
+ * index.
+ *
+ * @param n The index into the header field array
+ *
+ * @return The value of the specified header field, or null
+ * if the specified index is not valid.
+ */
+ public String getHeaderField(int n)
+ {
+ return headers.getHeaderFieldValueByIndex (n);
+ }
+
+ /**
+ * This method returns the header field key at the specified numeric
+ * index.
+ *
+ * @param n The index into the header field array
+ *
+ * @return The name of the header field key, or null
if the
+ * specified index is not valid.
+ */
+ public String getHeaderFieldKey(int n)
+ {
+ return headers.getHeaderFieldKeyByIndex (n);
+ }
}