From 8db76ca933c641a7b60834003cd77e4ee4b99c49 Mon Sep 17 00:00:00 2001 From: mkoch Date: Thu, 19 Jun 2003 15:08:22 +0000 Subject: [PATCH] 2003-06-19 Michael Koch * java/net/HttpURLConnection.java, java/net/Inet4Address.java, java/net/Inet6Address.java, java/net/SocketImpl.java, java/net/URLClassLoader.java: Reworked import statements. * java/net/InetAddress.java (getByAddress): Simplified. * java/net/ServerSocket.java (ServerSocket): Moved special handling during bind operation to bind(). (bind): Handle different cases when trying to bind a socket. * java/net/URLConnection.java (getHeaderFieldDate): Merged with classpath. (getHeaderFieldInt): Likewise. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@68198 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/ChangeLog | 18 +++++ libjava/java/net/HttpURLConnection.java | 12 ++-- libjava/java/net/Inet4Address.java | 2 +- libjava/java/net/Inet6Address.java | 2 +- libjava/java/net/InetAddress.java | 116 +++++++++++++++----------------- libjava/java/net/ServerSocket.java | 89 +++++++++++------------- libjava/java/net/SocketImpl.java | 9 ++- libjava/java/net/URLClassLoader.java | 4 +- libjava/java/net/URLConnection.java | 44 ++++++------ 9 files changed, 155 insertions(+), 141 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 6423b5d..245e855 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,5 +1,23 @@ 2003-06-19 Michael Koch + * java/net/HttpURLConnection.java, + java/net/Inet4Address.java, + java/net/Inet6Address.java, + java/net/SocketImpl.java, + java/net/URLClassLoader.java: + Reworked import statements. + * java/net/InetAddress.java + (getByAddress): Simplified. + * java/net/ServerSocket.java + (ServerSocket): Moved special handling during bind operation to + bind(). + (bind): Handle different cases when trying to bind a socket. + * java/net/URLConnection.java + (getHeaderFieldDate): Merged with classpath. + (getHeaderFieldInt): Likewise. + +2003-06-19 Michael Koch + * java/util/zip/InflaterInputStream.java (InflaterInputStream): Throw NullPointerException if in is null (as JDK does). diff --git a/libjava/java/net/HttpURLConnection.java b/libjava/java/net/HttpURLConnection.java index 4cdeaf1..2f12fe5 100644 --- a/libjava/java/net/HttpURLConnection.java +++ b/libjava/java/net/HttpURLConnection.java @@ -1,7 +1,6 @@ -// HttpURLConnection.java - Subclass of communications links using -// Hypertext Transfer Protocol. - -/* Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation +/* HttpURLConnection.java - Subclass of communications links using + Hypertext Transfer Protocol. + Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation This file is part of GNU Classpath. @@ -37,9 +36,12 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; -import java.io.*; +import java.io.InputStream; +import java.io.IOException; +import java.io.PushbackInputStream; import java.security.Permission; /* diff --git a/libjava/java/net/Inet4Address.java b/libjava/java/net/Inet4Address.java index d2892fe..bcfc46e 100644 --- a/libjava/java/net/Inet4Address.java +++ b/libjava/java/net/Inet4Address.java @@ -35,9 +35,9 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; -import java.io.IOException; import java.io.ObjectStreamException; import java.util.Arrays; diff --git a/libjava/java/net/Inet6Address.java b/libjava/java/net/Inet6Address.java index ee6e666..5514f67 100644 --- a/libjava/java/net/Inet6Address.java +++ b/libjava/java/net/Inet6Address.java @@ -35,9 +35,9 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; -import java.io.IOException; import java.util.Arrays; /** diff --git a/libjava/java/net/InetAddress.java b/libjava/java/net/InetAddress.java index aafa562..8ef6366 100644 --- a/libjava/java/net/InetAddress.java +++ b/libjava/java/net/InetAddress.java @@ -92,15 +92,15 @@ public class InetAddress implements Serializable // FIXME: implement this } - private void readObject(ObjectInputStream ois) + private void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException { - ois.defaultReadObject(); - addr = new byte[4]; - addr[3] = (byte) address; + ois.defaultReadObject (); + addr = new byte [4]; + addr [3] = (byte) address; for (int i = 2; i >= 0; --i) - addr[i] = (byte) (address >>= 8); + addr [i] = (byte) (address >>= 8); // Ignore family from serialized data. Since the saved address is 32 bits // the deserialized object will have an IPv4 address i.e. AF_INET family. @@ -110,7 +110,7 @@ public class InetAddress implements Serializable family = getFamily (addr); } - private void writeObject(ObjectOutputStream oos) throws IOException + private void writeObject (ObjectOutputStream oos) throws IOException { // Build a 32 bit address from the last 4 bytes of a 4 byte IPv4 address // or a 16 byte IPv6 address. @@ -118,9 +118,9 @@ public class InetAddress implements Serializable int i = len - 4; for (; i < len; i++) - address = address << 8 | (((int) addr[i]) & 0xFF); + address = address << 8 | (((int) addr [i]) & 0xFF); - oos.defaultWriteObject(); + oos.defaultWriteObject (); } private static native int getFamily (byte[] address); @@ -144,10 +144,10 @@ public class InetAddress implements Serializable int len = addr.length; if (len == 4) - return (addr[0] & 0xF0) == 0xE0; + return (addr [0] & 0xF0) == 0xE0; if (len == 16) - return addr[0] == (byte) 0xFF; + return addr [0] == (byte) 0xFF; return false; } @@ -174,7 +174,7 @@ public class InetAddress implements Serializable // This is the IPv4 implementation. // Any class derived from InetAddress should override this. - return addr[0] == 0x7F; + return addr [0] == 0x7F; } /** @@ -202,7 +202,7 @@ public class InetAddress implements Serializable // Any class derived from InetAddress should override this. // 10.0.0.0/8 - if (addr[0] == 0x0A) + if (addr [0] == 0x0A) return true; // XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here: @@ -263,9 +263,9 @@ public class InetAddress implements Serializable if (!isMulticastAddress ()) return false; - return (addr[0] == 0xE0 - && addr[1] == 0x00 - && addr[2] == 0x00); + return (addr [0] == 0xE0 + && addr [1] == 0x00 + && addr [2] == 0x00); } /** @@ -340,26 +340,26 @@ public class InetAddress implements Serializable { // An experiment shows that JDK1.2 returns a different byte array each // time. This makes sense, in terms of security. - return (byte[]) addr.clone(); + return (byte[]) addr.clone (); } /* Helper function due to a CNI limitation. */ private static InetAddress[] allocArray (int count) { - return new InetAddress[count]; + return new InetAddress [count]; } /* Helper function due to a CNI limitation. */ private static SecurityException checkConnect (String hostname) { - SecurityManager s = System.getSecurityManager(); + SecurityManager s = System.getSecurityManager (); if (s == null) return null; try { - s.checkConnect(hostname, -1); + s.checkConnect (hostname, -1); return null; } catch (SecurityException ex) @@ -375,7 +375,7 @@ public class InetAddress implements Serializable */ public String getHostAddress () { - StringBuffer sbuf = new StringBuffer(40); + StringBuffer sbuf = new StringBuffer (40); int len = addr.length; int i = 0; if (len == 16) @@ -383,37 +383,37 @@ public class InetAddress implements Serializable for (; ; i += 2) { if (i >= 16) - return sbuf.toString(); - int x = ((addr[i] & 0xFF) << 8) | (addr[i+1] & 0xFF); - boolean empty = sbuf.length() == 0; + return sbuf.toString (); + int x = ((addr [i] & 0xFF) << 8) | (addr [i + 1] & 0xFF); + boolean empty = sbuf.length () == 0; if (empty) { if (i == 10 && x == 0xFFFF) { // IPv4-mapped IPv6 address. - sbuf.append(":FFFF:"); + sbuf.append (":FFFF:"); break; // Continue as IPv4 address; } else if (i == 12) { // IPv4-compatible IPv6 address. - sbuf.append(':'); + sbuf.append (':'); break; // Continue as IPv4 address. } else if (i > 0) - sbuf.append("::"); + sbuf.append ("::"); } else - sbuf.append(':'); + sbuf.append (':'); if (x != 0 || i >= 14) - sbuf.append(Integer.toHexString(x).toUpperCase()); + sbuf.append (Integer.toHexString (x).toUpperCase ()); } } for ( ; ; ) { - sbuf.append(addr[i] & 0xFF); + sbuf.append (addr[i] & 0xFF); i++; if (i == len) break; - sbuf.append('.'); + sbuf.append ('.'); } return sbuf.toString(); @@ -422,7 +422,7 @@ public class InetAddress implements Serializable /** * Returns a hashcode of the InetAddress */ - public int hashCode() + public int hashCode () { // There hashing algorithm is not specified, but a simple experiment // shows that it is equal to the address, as a 32-bit big-endian integer. @@ -467,10 +467,10 @@ public class InetAddress implements Serializable /** * Returns then InetAddress as string */ - public String toString() + public String toString () { String result; - String address = getHostAddress(); + String address = getHostAddress (); if (hostName != null) result = hostName + "/" + address; @@ -492,16 +492,10 @@ public class InetAddress implements Serializable * * @since 1.4 */ - public static InetAddress getByAddress(byte[] addr) + public static InetAddress getByAddress (byte[] addr) throws UnknownHostException { - if (addr.length != 4 && addr.length != 16) - throw new UnknownHostException ("IP address has illegal length"); - - if (addr.length == 4) - return new Inet4Address (addr, null); - - return new Inet6Address (addr, null); + return getByAddress (null, addr); } /** @@ -553,7 +547,7 @@ public class InetAddress implements Serializable // Default to current host if necessary if (hostname == null) - return getLocalHost(); + return getLocalHost (); // Assume that the host string is an IP address byte[] address = aton (hostname); @@ -563,13 +557,13 @@ public class InetAddress implements Serializable return new Inet4Address (address, null); else if (address.length == 16) { - if ((address[10] == 0xFF) && (address[11] == 0xFF)) + if ((address [10] == 0xFF) && (address [11] == 0xFF)) { - byte[] ip4addr = new byte[4]; - ip4addr[0] = address[12]; - ip4addr[1] = address[13]; - ip4addr[2] = address[14]; - ip4addr[3] = address[15]; + byte[] ip4addr = new byte [4]; + ip4addr [0] = address [12]; + ip4addr [1] = address [13]; + ip4addr [2] = address [14]; + ip4addr [3] = address [15]; return new Inet4Address (ip4addr, null); } return new Inet6Address (address, null); @@ -580,7 +574,7 @@ public class InetAddress implements Serializable // Try to resolve the host by DNS InetAddress[] addresses = getAllByName (hostname); - return addresses[0]; + return addresses [0]; } /** @@ -603,8 +597,8 @@ public class InetAddress implements Serializable byte[] address = aton (hostname); if (address != null) { - InetAddress[] result = new InetAddress[1]; - result[0] = new InetAddress(address, null); + InetAddress[] result = new InetAddress [1]; + result [0] = new InetAddress (address, null); return result; } @@ -612,10 +606,10 @@ public class InetAddress implements Serializable return lookup (hostname, null, true); } - static final byte[] zeros = {0,0,0,0}; + static final byte[] zeros = { 0, 0, 0, 0 }; /* dummy InetAddress, used to bind socket to any (all) network interfaces */ - static final InetAddress ANY_IF = new InetAddress(zeros, null); + static final InetAddress ANY_IF = new InetAddress (zeros, null); private static final byte[] localhostAddress = { 127, 0, 0, 1 }; @@ -629,28 +623,28 @@ public class InetAddress implements Serializable * @exception UnknownHostException If no IP address for the host could * be found */ - public static InetAddress getLocalHost() throws UnknownHostException + public static InetAddress getLocalHost () throws UnknownHostException { - SecurityManager s = System.getSecurityManager(); + SecurityManager s = System.getSecurityManager (); // Experimentation shows that JDK1.2 does cache the result. // However, if there is a security manager, and the cached result // is other than "localhost", we need to check again. if (localhost == null || (s != null && localhost.addr != localhostAddress)) - getLocalHost(s); + getLocalHost (s); return localhost; } - private static synchronized void getLocalHost(SecurityManager s) + private static synchronized void getLocalHost (SecurityManager s) throws UnknownHostException { // Check the localhost cache again, now that we've synchronized. if (s == null && localhost != null) return; - String hostname = getLocalHostname(); + String hostname = getLocalHostname (); if (s != null) { @@ -664,7 +658,7 @@ public class InetAddress implements Serializable { // This is wrong, if the name returned from getLocalHostname() // is not a fully qualified name. FIXME. - s.checkConnect(hostname, -1); + s.checkConnect (hostname, -1); } catch (SecurityException ex) { @@ -676,8 +670,8 @@ public class InetAddress implements Serializable { try { - localhost = new InetAddress(null, null); - lookup(hostname, localhost, false); + localhost = new InetAddress (null, null); + lookup (hostname, localhost, false); } catch (Exception ex) { diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java index 2d04eac..8dd420b 100644 --- a/libjava/java/net/ServerSocket.java +++ b/libjava/java/net/ServerSocket.java @@ -157,57 +157,11 @@ public class ServerSocket if (impl == null) throw new IOException("Cannot initialize Socket implementation"); - SecurityManager s = System.getSecurityManager(); - if (s != null) - s.checkListen(port); - - if (bindAddr == null) - bindAddr = InetAddress.ANY_IF; - // create socket impl.create(true); - // bind to address/port - try - { - impl.bind(bindAddr, port); - } - catch (IOException exception) - { - impl.close(); - throw exception; - } - catch (RuntimeException exception) - { - impl.close(); - throw exception; - } - catch (Error error) - { - impl.close(); - throw error; - } - - // listen on socket - try - { - impl.listen(backlog); - } - catch (IOException exception) - { - impl.close(); - throw exception; - } - catch (RuntimeException exception) - { - impl.close(); - throw exception; - } - catch (Error error) - { - impl.close(); - throw error; - } + // bind/listen socket + bind (new InetSocketAddress (bindAddr, port), backlog); } /** @@ -258,9 +212,48 @@ public class ServerSocket if (s != null) s.checkListen (tmp.getPort ()); + // bind to address/port + try + { impl.bind (tmp.getAddress (), tmp.getPort ()); + } + catch (IOException exception) + { + impl.close(); + throw exception; + } + catch (RuntimeException exception) + { + impl.close(); + throw exception; + } + catch (Error error) + { + impl.close(); + throw error; + } + + // listen on socket + try + { impl.listen(backlog); } + catch (IOException exception) + { + impl.close(); + throw exception; + } + catch (RuntimeException exception) + { + impl.close(); + throw exception; + } + catch (Error error) + { + impl.close(); + throw error; + } + } /** * This method returns the local address to which this socket is bound diff --git a/libjava/java/net/SocketImpl.java b/libjava/java/net/SocketImpl.java index 25ffe91..d380d8c 100644 --- a/libjava/java/net/SocketImpl.java +++ b/libjava/java/net/SocketImpl.java @@ -1,5 +1,6 @@ /* SocketImpl.java -- Abstract socket implementation class - Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 + Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -35,9 +36,13 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; -import java.io.*; +import java.io.FileDescriptor; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; /* Written using on-line Java Platform 1.2 API Specification. * Believed complete and correct. diff --git a/libjava/java/net/URLClassLoader.java b/libjava/java/net/URLClassLoader.java index d7fc77f..10b6735 100644 --- a/libjava/java/net/URLClassLoader.java +++ b/libjava/java/net/URLClassLoader.java @@ -35,14 +35,13 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ + package java.net; import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FilterInputStream; import java.io.FilePermission; import java.io.InputStream; import java.io.IOException; @@ -60,7 +59,6 @@ import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.zip.ZipException; /** * A secure class loader that can load classes and resources from diff --git a/libjava/java/net/URLConnection.java b/libjava/java/net/URLConnection.java index 7d2ab44..3b8a85b 100644 --- a/libjava/java/net/URLConnection.java +++ b/libjava/java/net/URLConnection.java @@ -331,20 +331,19 @@ public abstract class URLConnection */ public int getHeaderFieldInt(String name, int defaultValue) { - String str = getHeaderField(name); - int result = defaultValue; + String value = getHeaderField (name); + if (value == null) + return defaultValue; + try { - if (str != null) - result = Integer.parseInt (str); + return Integer.parseInt (value); } catch (NumberFormatException e) { - ; // Do nothing; defaultValue is the default. + return defaultValue; } - - return result; } /** @@ -353,27 +352,32 @@ public abstract class URLConnection * value if the field is not present or cannot be converted to a date. * * @param name The name of the header field - * @param val The dafault date + * @param defaultValue The default date if the header field is not found + * or can't be converted. * * @return Returns the date value of the header filed or the default value * if the field is missing or malformed */ - public long getHeaderFieldDate(String name, long val) + public long getHeaderFieldDate (String name, long defaultValue) { if (! dateformats_initialized) - initializeDateFormats(); - String str = getHeaderField(name); + initializeDateFormats (); + + long result = defaultValue; + String str = getHeaderField (name); + if (str != null) { - Date date; - if ((date = dateFormat1.parse(str, new ParsePosition(0))) != null) - val = date.getTime(); - else if ((date = dateFormat2.parse(str, new ParsePosition(0))) != null) - val = date.getTime(); - else if ((date = dateFormat3.parse(str, new ParsePosition(0))) != null) - val = date.getTime(); + Date date; + if ((date = dateFormat1.parse (str, new ParsePosition (0))) != null) + result = date.getTime (); + else if ((date = dateFormat2.parse (str, new ParsePosition (0))) != null) + result = date.getTime (); + else if ((date = dateFormat3.parse (str, new ParsePosition (0))) != null) + result = date.getTime (); } - return val; + + return result; } /** @@ -387,7 +391,7 @@ public abstract class URLConnection * @return The header field key or null if index is past the end * of the headers. */ - public String getHeaderFieldKey(int index) + public String getHeaderFieldKey (int index) { // Subclasses for specific protocols override this. return null; -- 2.7.4