This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
some patches for 1.3 compatibility
- To: java-patches at gcc dot gnu dot org
- Subject: some patches for 1.3 compatibility
- From: Tony Kimball <alk at pobox dot com>
- Date: Thu, 29 Mar 2001 19:47:54 -0600 (CST)
- Reply-To: alk at pobox dot com
Porting an app, I found these patches useful. The Win32 stuff is not
tested at all yet.
Index: libjava/java/net/PlainSocketImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/PlainSocketImpl.java,v
retrieving revision 1.8
diff -u -r1.8 PlainSocketImpl.java
--- PlainSocketImpl.java 2000/12/08 10:28:32 1.8
+++ PlainSocketImpl.java 2001/03/30 01:45:46
@@ -35,6 +35,10 @@
_Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
_Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
+ private boolean shutdown_input = false;
+
+ private boolean shutdown_output = false;
+
int fnum = -1;
// This value is set/read by setOption/getOption.
@@ -73,6 +77,8 @@
protected InputStream getInputStream() throws IOException
{
// FIXME: TODO - Implement class SocketInputStream timeouts in read();
+ if (shutdown_input)
+ throw new IOException("Socket input stream is shut down");
if (in == null)
in = new FileInputStream (fd);
return in;
@@ -80,6 +86,8 @@
protected OutputStream getOutputStream() throws IOException
{
+ if (shutdown_output)
+ throw new IOException("Socket output stream is shut down");
if (out == null)
out = new FileOutputStream (fd);
return out;
@@ -95,4 +103,21 @@
if (fd.valid())
fd.close();
}
+
+ protected void shutdownInput() throws IOException
+ {
+ if (fd.valid())
+ fd.shutdownInput();
+ if (in != null)
+ in.setEOF();
+ shutdown_input = true;
+ }
+
+ protected void shutdownOutput() throws IOException
+ {
+ if (fd.valid())
+ fd.shutdownOutput();
+ shutdown_output = true;
+ }
+
}
Index: libjava/java/net/Socket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/Socket.java,v
retrieving revision 1.7
diff -u -r1.7 Socket.java
--- Socket.java 2000/03/07 19:55:27 1.7
+++ Socket.java 2001/03/30 01:45:46
@@ -250,6 +250,16 @@
impl.close();
}
+ public synchronized void shutdownInput() throws IOException
+ {
+ impl.shutdownInput();
+ }
+
+ public synchronized void shutdownOutput() throws IOException
+ {
+ impl.shutdownOutput();
+ }
+
public String toString ()
{
return "Socket" + impl.toString();
Index: libjava/java/net/SocketImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/SocketImpl.java,v
retrieving revision 1.5
diff -u -r1.5 SocketImpl.java
--- SocketImpl.java 2000/03/07 19:55:27 1.5
+++ SocketImpl.java 2001/03/30 01:45:47
@@ -55,6 +55,10 @@
protected abstract void close () throws IOException;
+ protected abstract void shutdownInput () throws IOException;
+
+ protected abstract void shutdownOutput () throws IOException;
+
protected FileDescriptor getFileDescriptor () { return fd; }
protected InetAddress getInetAddress () { return address; }
Index: libjava/java/net/URL.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URL.java,v
retrieving revision 1.7
diff -u -r1.7 URL.java
--- URL.java 2000/09/08 19:37:09 1.7
+++ URL.java 2001/03/30 01:45:47
@@ -31,6 +31,8 @@
private String host;
private int port = -1; // Initialize for constructor using context.
private String file;
+ private String path;
+ private String query;
private String ref;
private int hashCode = 0;
transient private URLStreamHandler handler;
@@ -93,6 +95,18 @@
this.file = file.substring(0, hashAt);
this.ref = file.substring(hashAt + 1);
}
+
+ int queryAt = file.lastIndexOf('?');
+ if (queryAt < 0)
+ {
+ this.path = file;
+ }
+ else
+ {
+ this.path = file.substring(0, queryAt);
+ this.query = file.substring(++queryAt);
+ }
+
hashCode = hashCode(); // Used for serialization.
}
@@ -217,6 +231,16 @@
public String getFile()
{
return file;
+ }
+
+ public String getPath()
+ {
+ return path;
+ }
+
+ public String getQuery()
+ {
+ return query;
}
public String getHost()
Index: libjava/java/io/BufferedInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/BufferedInputStream.java,v
retrieving revision 1.3
diff -u -r1.3 BufferedInputStream.java
--- BufferedInputStream.java 2000/03/07 19:55:26 1.3
+++ BufferedInputStream.java 2001/03/30 01:45:47
@@ -35,6 +35,8 @@
/* The maximum read-ahead allowed before calls to reset() fail. */
protected int marklimit = 0;
+ private boolean isEOF = false;
+
public BufferedInputStream(InputStream in)
{
this(in, 2048);
@@ -73,7 +75,7 @@
public synchronized int read() throws IOException
{
- if (pos >= count && !refill())
+ if (isEOF || (pos >= count && !refill()))
return -1; // EOF
if (markpos >= 0 && pos - markpos > marklimit)
@@ -87,7 +89,7 @@
if (off < 0 || len < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
- if (pos >= count && !refill())
+ if (isEOF || (pos >= count && !refill()))
return -1; // No bytes were read before EOF.
int remain = Math.min(count - pos, len);
@@ -112,6 +114,9 @@
{
final long origN = n;
+ if (isEOF)
+ throw new EOFException();
+
while (n > 0L)
{
if (pos >= count && !refill())
@@ -156,6 +161,9 @@
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
+
+ if (isEOF)
+ return false;
int numread = super.read(buf, count, buf.length - count);
Index: libjava/java/io/ByteArrayInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ByteArrayInputStream.java,v
retrieving revision 1.5
diff -u -r1.5 ByteArrayInputStream.java
--- ByteArrayInputStream.java 2001/02/19 05:37:28 1.5
+++ ByteArrayInputStream.java 2001/03/30 01:45:47
@@ -71,14 +71,14 @@
public synchronized int read()
{
- if (pos < count)
+ if (pos < count && !isEOF)
return ((int) buf[pos++]) & 0xFF;
return -1;
}
public synchronized int read(byte[] b, int off, int len)
{
- if (pos >= count)
+ if (isEOF || pos >= count)
return -1;
int numBytes = Math.min(count - pos, len);
@@ -94,11 +94,14 @@
public synchronized long skip(long n)
{
+ if (isEOF)
+ return 0;
+
// Even though the var numBytes is a long, in reality it can never
// be larger than an int since the result of subtracting 2 positive
// ints will always fit in an int. Since we have to return a long
// anyway, numBytes might as well just be a long.
- long numBytes = Math.min((long) (count - pos), n < 0 ? 0L : n);
+ long numBytes = Math.min((long) (count - pos), (isEOF || n < 0) ? 0L : n);
pos += numBytes;
return numBytes;
}
Index: libjava/java/io/DataInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/DataInputStream.java,v
retrieving revision 1.10
diff -u -r1.10 DataInputStream.java
--- DataInputStream.java 2001/02/17 15:09:46 1.10
+++ DataInputStream.java 2001/03/30 01:45:47
@@ -672,6 +672,7 @@
catch (EOFException x)
{
// do nothing.
+ n = 0;
}
return n;
}
Index: libjava/java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.6
diff -u -r1.6 FileDescriptor.java
--- FileDescriptor.java 2000/12/26 00:24:01 1.6
+++ FileDescriptor.java 2001/03/30 01:45:47
@@ -54,7 +54,10 @@
native void write (byte[] b, int offset, int len)
throws IOException, NullPointerException, IndexOutOfBoundsException;
native void close () throws IOException;
+ native void shutdownInput () throws IOException;
+ native void shutdownOutput () throws IOException;
native int seek (long pos, int whence) throws IOException;
+ native void setLength (long pos) throws IOException;
native long length () throws IOException;
native long getFilePointer () throws IOException;
native int read () throws IOException;
Index: libjava/java/io/FileInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileInputStream.java,v
retrieving revision 1.5
diff -u -r1.5 FileInputStream.java
--- FileInputStream.java 2000/12/08 10:28:32 1.5
+++ FileInputStream.java 2001/03/30 01:45:47
@@ -70,11 +70,15 @@
public int read() throws IOException
{
+ if (isEOF)
+ return -1;
return fd.read();
}
public int read(byte[] b) throws IOException
{
+ if (isEOF)
+ return -1;
return fd.read(b, 0, b.length);
}
@@ -83,11 +87,18 @@
if (off < 0 || len < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
+ if (isEOF)
+ return -1;
+
return fd.read(b, off, len);
}
public long skip(long n) throws IOException
{
+ if (isEOF)
+ throw new EOFException();
+
return fd.seek(n, FileDescriptor.CUR);
}
+
}
Index: libjava/java/io/InputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/InputStream.java,v
retrieving revision 1.3
diff -u -r1.3 InputStream.java
--- InputStream.java 2000/03/07 19:55:26 1.3
+++ InputStream.java 2001/03/30 01:45:47
@@ -20,10 +20,16 @@
public abstract class InputStream
{
+ protected boolean isEOF = false;
+
public InputStream()
{
}
+ public void setEOF() {
+ isEOF = true;
+ }
+
public int available() throws IOException
{
return 0;
@@ -48,6 +54,8 @@
public int read(byte[] b) throws IOException
{
+ if (isEOF)
+ return -1;
return read(b, 0, b.length);
}
@@ -55,6 +63,10 @@
{
if (off < 0 || len < 0 || off + len > b.length)
throw new IndexOutOfBoundsException();
+
+ if (isEOF)
+ return -1;
+
if (b.length == 0)
return 0;
Index: libjava/java/io/LineNumberInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/LineNumberInputStream.java,v
retrieving revision 1.3
diff -u -r1.3 LineNumberInputStream.java
--- LineNumberInputStream.java 2000/03/07 19:55:26 1.3
+++ LineNumberInputStream.java 2001/03/30 01:45:47
@@ -87,6 +87,9 @@
if (off < 0 || len < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
+ if (isEOF)
+ return -1;
+
// This case always succeeds.
if (len == 0)
return 0;
@@ -123,6 +126,9 @@
public long skip(long n) throws IOException
{
+ if (isEOF)
+ throw new EOFException();
+
if (n <= 0)
return 0L;
Index: libjava/java/io/PipedInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/PipedInputStream.java,v
retrieving revision 1.9
diff -u -r1.9 PipedInputStream.java
--- PipedInputStream.java 2001/01/06 23:28:40 1.9
+++ PipedInputStream.java 2001/03/30 01:45:47
@@ -263,6 +263,9 @@
if (closed)
throw new IOException ("Pipe closed");
+ if (isEOF)
+ return -1;
+
// If the buffer is empty, wait until there is something in the pipe
// to read.
try
Index: libjava/java/io/PushbackInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/PushbackInputStream.java,v
retrieving revision 1.4
diff -u -r1.4 PushbackInputStream.java
--- PushbackInputStream.java 2000/06/27 21:27:50 1.4
+++ PushbackInputStream.java 2001/03/30 01:45:47
@@ -58,6 +58,9 @@
public int read() throws IOException
{
+ if (isEOF)
+ return -1;
+
if (pos < buf.length)
return ((int) buf[pos++]) & 0xFF;
@@ -66,6 +69,9 @@
public int read(byte[] b, int off, int len) throws IOException
{
+ if (isEOF)
+ return -1;
+
if (off < 0 || len < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
@@ -112,6 +118,9 @@
public long skip(long n) throws IOException
{
final long origN = n;
+
+ if (isEOF)
+ throw new EOFException();
if (n > 0L)
{
Index: libjava/java/io/RandomAccessFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/RandomAccessFile.java,v
retrieving revision 1.5
diff -u -r1.5 RandomAccessFile.java
--- RandomAccessFile.java 2000/12/08 10:28:32 1.5
+++ RandomAccessFile.java 2001/03/30 01:45:47
@@ -40,6 +40,12 @@
return fd.getFilePointer();
}
+ public void setLength (long pos) throws IOException
+ {
+ fd.setLength(pos);
+ return;
+ }
+
public long length () throws IOException
{
return fd.length();
Index: libjava/java/io/StringBufferInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/StringBufferInputStream.java,v
retrieving revision 1.3
diff -u -r1.3 StringBufferInputStream.java
--- StringBufferInputStream.java 2000/03/07 19:55:26 1.3
+++ StringBufferInputStream.java 2001/03/30 01:45:47
@@ -36,25 +36,25 @@
count = s.length();
}
- public int available()
+ public synchronized int available()
{
return count - pos;
}
- public int read()
+ public synchronized int read()
{
- if (pos >= count)
+ if (isEOF || pos >= count)
return -1; // EOF
return ((int) buffer.charAt(pos++)) & 0xFF;
}
- public int read(byte[] b, int off, int len)
+ public synchronized int read(byte[] b, int off, int len)
{
if (off < 0 || len < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
- if (pos >= count)
+ if (isEOF || pos >= count)
return -1; // EOF
int numRead = Math.min(len, count - pos);
@@ -66,14 +66,14 @@
return numRead;
}
- public void reset()
+ public synchronized void reset()
{
pos = 0;
}
- public long skip(long n)
+ public synchronized long skip(long n)
{
- if (n < 0)
+ if (isEOF || n < 0)
return 0L;
long actualSkip = Math.min(n, count - pos);
Index: libjava/java/io/natFileDescriptorEcos.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorEcos.cc,v
retrieving revision 1.5
diff -u -r1.5 natFileDescriptorEcos.cc
--- natFileDescriptorEcos.cc 2001/03/26 07:05:32 1.5
+++ natFileDescriptorEcos.cc 2001/03/30 01:45:47
@@ -87,6 +87,16 @@
{
}
+void
+java::io::FileDescriptor::shutdownInput (void)
+{
+}
+
+void
+java::io::FileDescriptor::shutdownOutput (void)
+{
+}
+
jint
java::io::FileDescriptor::seek (jlong pos, jint whence)
{
@@ -99,6 +109,12 @@
throw new EOFException;
return 0;
+}
+
+void
+java::io::FileDescriptor::setLength (void)
+{
+ return;
}
jlong
Index: libjava/java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.12
diff -u -r1.12 natFileDescriptorPosix.cc
--- natFileDescriptorPosix.cc 2001/03/26 07:05:32 1.12
+++ natFileDescriptorPosix.cc 2001/03/30 01:45:47
@@ -17,6 +17,7 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/param.h>
+#include <sys/socket.h>
#include <fcntl.h>
#ifdef HAVE_SYS_IOCTL_H
@@ -164,6 +165,20 @@
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
+void
+java::io::FileDescriptor::shutdownInput (void)
+{
+ if (::shutdown(fd,0))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+}
+
+void
+java::io::FileDescriptor::shutdownOutput (void)
+{
+ if (::shutdown(fd,1))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+}
+
jint
java::io::FileDescriptor::seek (jlong pos, jint whence)
{
@@ -188,6 +203,36 @@
if (::fstat (fd, &sb))
throw new IOException (JvNewStringLatin1 (strerror (errno)));
return sb.st_size;
+}
+
+void
+java::io::FileDescriptor::setLength (jlong pos)
+{
+ struct stat sb;
+ off_t orig;
+
+ if (::fstat (fd, &sb))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+
+ if ((jlong)sb.st_size == pos)
+ return;
+
+ orig = ::lseek(fd, (off_t) 0, SEEK_CUR);
+ if (orig == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+
+ if ((jlong)sb.st_size < pos) {
+ if (::lseek(fd, (off_t)pos, SEEK_SET) == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ if (::lseek(fd, orig, SEEK_SET) == -1)
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ return;
+ }
+
+ if (::ftruncate (fd, (off_t) pos))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+
+ return;
}
jlong
Index: libjava/java/io/natFileDescriptorWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorWin32.cc,v
retrieving revision 1.2
diff -u -r1.2 natFileDescriptorWin32.cc
--- natFileDescriptorWin32.cc 2001/03/26 07:05:32 1.2
+++ natFileDescriptorWin32.cc 2001/03/30 01:45:47
@@ -56,6 +56,31 @@
return (char *)last;
}
+static char *
+winsockerr (void)
+{
+ static LPVOID last = NULL;
+ LPVOID old = NULL;
+
+ if (last)
+ old = last;
+
+ FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ WSAGetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR) &last,
+ 0,
+ NULL);
+
+ if (old)
+ LocalFree (old);
+
+ return (char *)last;
+}
+
jboolean
java::io::FileDescriptor::valid (void) {
BY_HANDLE_FILE_INFORMATION info;
@@ -170,6 +195,20 @@
throw new IOException (JvNewStringLatin1 (winerr ()));
}
+void
+java::io::FileDescriptor::shutdownInput (void)
+{
+ if (shutdown((SOCKET)fd,SD_RECEIVE,&err) != 0)
+ throw new IOException (JvNewStringLatin1 (winsockerr ()));
+}
+
+void
+java::io::FileDescriptor::shutdownOutput (void)
+{
+ if (shutdown((SOCKET)fd,SD_SEND,&err) != 0)
+ throw new IOException (JvNewStringLatin1 (winsockerr ()));
+}
+
jint
java::io::FileDescriptor::seek (jlong pos, jint whence)
{
@@ -205,8 +244,55 @@
DWORD low;
low = GetFileSize ((HANDLE)fd, &high);
- // FIXME: Error checking
+ if ((low == INVALID_FILE_SIZE) && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
return (((jlong)high) << 32L) | (jlong)low;
+}
+
+void
+java::io::FileDescriptor::setLength(jlong pos)
+{
+ LONG_INTEGER liOrigFilePointer;
+ LONG_INTEGER liNewFilePointer;
+ LONG_INTEGER liEndFilePointer;
+
+ // get the original file pointer
+ if (SetFilePointerEx((HANDLE)fd, (LONG_INTEGER)0, &liOrigFilePointer, FILE_CURRENT) != (BOOL)0
+ && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+
+ // get the length of the file
+ if (SetFilePointerEx((HANDLE)fd, (LONG_INTEGER)0, &liEndFilePointer, FILE_END) != (BOOL)0
+ && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+
+ if ((jlong)liEndFilePointer == pos) {
+ // restore the file pointer
+ if (liOrigFilePointer != liEndFilePointer) {
+ if (SetFilePointerEx((HANDLE)fd, liOrigFilePointer, &liNewFilePointer, FILE_BEGIN) != (BOOL)0
+ && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+ }
+ return;
+ }
+
+ // seek to the new end of file
+ if (SetFilePointerEx((HANDLE)fd, (LONG_INTEGER)pos, &liNewFilePointer, FILE_BEGIN) != (BOOL)0
+ && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+
+ // truncate the file at this point
+ if (SetEndOfFile((HANDLE)fd) != (BOOL)0 && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+
+ if (liOrigFilePointer < liNewFilePointer) {
+ // restore the file pointer
+ if (SetFilePointerEx((HANDLE)fd, liOrigFilePointer, &liNewFilePointer, FILE_BEGIN) != (BOOL)0
+ && (GetLastError() != NO_ERROR))
+ throw new IOException (JvNewStringLatin1 (winerr ()));
+ }
+
+ return;
}
jint