This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: FYI: Classpath merge of HttpURLConnection


Hi,

On Sun, 2002-11-17 at 16:46, Mark Wielaard wrote:
> The following merges HttpURLConnection with Classpath. It is mainly
> adding documentation, but I took the implementation of setRequestMethod
> and getErrorStream() from the Classpath implementation.
> 
> 2002-11-17  Mark Wielaard  <mark@klomp.org>
> 
>         * java/net/HttpURLConnection.java: Merge with GNU Classpath.

I had committed a bit to fast. I had missed the actual getErrorStream()
and the modified getPermission() implementations. Here they are.

2002-11-17  Mark Wielaard <mark@klomp.org>

	* java/net/HttpURLConnection.java (getPermission): Take port
	into consideration.
	(getErrorStream): Implement.

Cheers,

Mark
Index: java/net/HttpURLConnection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/HttpURLConnection.java,v
retrieving revision 1.8
diff -u -r1.8 HttpURLConnection.java
--- java/net/HttpURLConnection.java	17 Nov 2002 15:47:27 -0000	1.8
+++ java/net/HttpURLConnection.java	17 Nov 2002 16:02:33 -0000
@@ -495,17 +495,62 @@
    */
   public Permission getPermission() throws IOException
   {
-    return new SocketPermission (url.getHost (), "connect");
+    URL url = getURL();
+    String host = url.getHost();
+    int port = url.getPort();
+    if (port == -1)
+      port = 80;
+    
+    host = host + ":" + port;
+    
+    return new SocketPermission(host, "connect");
   }
 
   /**
-   * Returns the error stream if the connection failed but the server sent
-   * useful data nonetheless
+   * This method allows the caller to retrieve any data that might have
+   * been sent despite the fact that an error occurred.  For example, the
+   * HTML page sent along with a 404 File Not Found error.  If the socket
+   * is not connected, or if no error occurred or no data was returned,
+   * this method returns <code>null</code>.
+   *
+   * @return An <code>InputStream</code> for reading error data.
    */
   public InputStream getErrorStream ()
   {
-    // FIXME: implement this
-    return null;
+    if (!connected)
+      return(null);
+    
+    int code;
+    try 
+      {
+	code = getResponseCode();
+      }
+    catch(IOException e)
+      {
+	code = -1;
+      }
+    
+    if (code == -1)
+      return(null);
+    
+    if (((code/100) != 4) || ((code/100) != 5))
+      return(null); 
+    
+    try
+      {
+	PushbackInputStream pbis = new PushbackInputStream(getInputStream());
+	
+	int i = pbis.read();
+	if (i == -1)
+	  return(null);
+	
+	pbis.unread(i);
+	return(pbis);
+      }
+    catch(IOException e)
+      {
+	return(null);
+      }
   }
 
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]