From 93920ae62a07fe6cd69e0893b8f0edf7cfdf6adc Mon Sep 17 00:00:00 2001 From: jfrijters Date: Fri, 10 Feb 2006 12:51:05 +0000 Subject: [PATCH] *** empty log message *** --- classpath/allsources.lst | 1 + .../java/net/protocol/ikvmres/Handler.java | 203 +--------------- classpath/ikvm/io/InputStreamWrapper.java | 223 +++++++++++++++++ runtime/classpath.cs | 230 +++++++++++++++++- 4 files changed, 463 insertions(+), 194 deletions(-) create mode 100644 classpath/ikvm/io/InputStreamWrapper.java diff --git a/classpath/allsources.lst b/classpath/allsources.lst index bb5f8d96..9c6ce510 100644 --- a/classpath/allsources.lst +++ b/classpath/allsources.lst @@ -4213,6 +4213,7 @@ gnu/java/nio/VMPipe.java ikvm/internal/AnnotationAttributeBase.java ikvm/internal/Library.java ikvm/internal/LibraryVMInterface.java +ikvm/io/InputStreamWrapper.java ikvm/lang/CIL.java java/io/VMFile.java java/io/VMObjectInputStream.java diff --git a/classpath/gnu/java/net/protocol/ikvmres/Handler.java b/classpath/gnu/java/net/protocol/ikvmres/Handler.java index 69296d97..85dc9331 100644 --- a/classpath/gnu/java/net/protocol/ikvmres/Handler.java +++ b/classpath/gnu/java/net/protocol/ikvmres/Handler.java @@ -1,5 +1,5 @@ /* - Copyright (C) 2002, 2003, 2004, 2005 Jeroen Frijters + Copyright (C) 2002, 2003, 2004, 2005, 2006 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -24,153 +24,9 @@ package gnu.java.net.protocol.ikvmres; -import cli.System.Resources.*; -import cli.System.Reflection.*; -import cli.System.Collections.*; +import cli.System.Reflection.Assembly; import java.net.*; import java.io.*; -import java.io.IOException; - -class LZInputStream extends FilterInputStream -{ - private int[] ptr_tbl; - private int[] char_tbl; - private int[] stack; - private int table_size; - private int count; - private int bitoff; - private int bitbuf; - private int prev = -1; - private int bits; - private int cc; - private int fc; - private int sp; - - public LZInputStream(InputStream in) throws IOException - { - super(in); - bitoff = 0; - count = 0; - table_size = 256; - bits = 9; - ptr_tbl = new int[table_size]; - char_tbl = new int[table_size]; - stack = new int[table_size]; - sp = 0; - cc = prev = incode(); - stack[sp++] = cc; - } - - public int read() throws IOException - { - if (sp == 0) - { - if (stack.length != table_size) - { - stack = new int[table_size]; - } - int ic = cc = incode(); - if (cc == -1) - { - return -1; - } - if (count >= 0 && cc >= count + 256) - { - stack[sp++] = fc; - cc = prev; - ic = find(prev, fc); - } - while (cc >= 256) - { - stack[sp++] = char_tbl[cc - 256]; - cc = ptr_tbl[cc - 256]; - } - fc = stack[sp++] = cc; - if (count >= 0) - { - ptr_tbl[count] = prev; - char_tbl[count] = fc; - } - count++; - if (count == table_size) - { - count = -1; - if (bits == 12) - { - table_size = 256; - bits = 9; - } - else - { - bits++; - table_size = (1 << bits) - 256; - } - ptr_tbl = null; - char_tbl = null; - ptr_tbl = new int[table_size]; - char_tbl= new int[table_size]; - } - prev = ic; - } - return stack[--sp] & 0xFF; - } - - private int find(int p, int c) - { - int i; - for (i = 0; i < count; i++) - { - if (ptr_tbl[i] == p && char_tbl[i] == c) - { - break; - } - } - return i + 256; - } - - private int incode() throws IOException - { - while (bitoff < bits) - { - int v = in.read(); - if (v == -1) - { - return -1; - } - bitbuf |= (v & 0xFF) << bitoff; - bitoff += 8; - } - bitoff -= bits; - int result = bitbuf; - bitbuf >>= bits; - result -= bitbuf << bits; - return result; - } - - public int read(byte[] b) throws IOException - { - return read(b, 0, b.length); - } - - public int read(byte[] b, int off, int len) throws IOException - { - if(len == 0) - { - return 0; - } - int i = 0; - for (; i < len ; i++) - { - int r = read(); - if(r == -1) - { - break; - } - b[off + i] = (byte)r; - } - return (i == 0) ? -1 : i; - } -} class IkvmresURLConnection extends URLConnection { @@ -182,8 +38,6 @@ class IkvmresURLConnection extends URLConnection doOutput = false; } - static native String MangleResourceName(String name); - public void connect() throws IOException { if(!connected) @@ -260,60 +114,29 @@ public class Handler extends URLStreamHandler public static InputStream readResourceFromAssembly(Assembly asm, String resource) throws IOException { - resource = resource.substring(1); - cli.System.IO.Stream s; try { if(false) throw new cli.System.Security.SecurityException(); - s = asm.GetManifestResourceStream(IkvmresURLConnection.MangleResourceName(resource)); - if(s == null) - { - throw new FileNotFoundException("resource " + resource + " not found in assembly " + asm.get_FullName()); - } + if(false) throw new cli.System.IO.FileNotFoundException(); + if(false) throw new cli.System.IO.IOException(); + return new ikvm.io.InputStreamWrapper(ReadResourceFromAssemblyImpl(asm, resource)); } - catch(cli.System.Security.SecurityException x2) + catch (cli.System.Security.SecurityException x) { - throw (IOException)new IOException().initCause(x2); + throw (IOException)new IOException().initCause(x); } - try + catch (cli.System.IO.FileNotFoundException x) { - Object r = new ResourceReader(s); - try - { - IEnumerator e = ((IEnumerable)r).GetEnumerator(); - if(!e.MoveNext()) - { - throw new IOException("Invalid resource " + resource + " found in assembly " + asm.get_FullName()); - } - DictionaryEntry de = (DictionaryEntry)e.get_Current(); - String key = (String)de.get_Key(); - byte[] value = (byte[])de.get_Value(); - InputStream inputStream = new ByteArrayInputStream(value); - if(key.equals("lz")) - { - inputStream = new LZInputStream(inputStream); - } - else if(key.equals("ikvm")) - { - // not compressed - } - else - { - throw new IOException("Unsupported resource encoding " + key + " for resource " + resource + " found in assembly " + asm.get_FullName()); - } - return inputStream; - } - finally - { - ((cli.System.IDisposable)r).Dispose(); - } + throw (FileNotFoundException)new FileNotFoundException().initCause(x); } - finally + catch(cli.System.IO.IOException x) { - s.Close(); + throw (IOException)new IOException().initCause(x); } } + private static native cli.System.IO.Stream ReadResourceFromAssemblyImpl(Assembly asm, String resource); + protected URLConnection openConnection(URL url) throws IOException { return new IkvmresURLConnection(url); diff --git a/classpath/ikvm/io/InputStreamWrapper.java b/classpath/ikvm/io/InputStreamWrapper.java new file mode 100644 index 00000000..68a4a63f --- /dev/null +++ b/classpath/ikvm/io/InputStreamWrapper.java @@ -0,0 +1,223 @@ +/* + Copyright (C) 2006 Jeroen Frijters + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jeroen Frijters + jeroen@frijters.net + +*/ + +package ikvm.io; + +public final class InputStreamWrapper extends java.io.InputStream +{ + private cli.System.IO.Stream stream; + private long markPosition = -1; + + public InputStreamWrapper(cli.System.IO.Stream stream) + { + this.stream = stream; + } + + public int read() throws java.io.IOException + { + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + return stream.ReadByte(); + } + catch (cli.System.IO.IOException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + catch (cli.System.ObjectDisposedException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + } + + public int read(byte[] b) throws java.io.IOException + { + return read(b, 0, b.length); + } + + public int read(byte[] b, int off, int len) throws java.io.IOException + { + if (b == null) + { + throw new NullPointerException(); + } + if (off < 0 || len < 0 || b.length - off < len) + { + throw new IndexOutOfBoundsException(); + } + if (len == 0) + { + return 0; + } + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + int count = stream.Read(b, off, len); + return count == 0 ? -1 : count; + } + catch (cli.System.IO.IOException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + catch (cli.System.ObjectDisposedException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + } + + public long skip(long n) throws java.io.IOException + { + if (n <= 0) + { + return 0; + } + else if (stream.get_CanSeek()) + { + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + long pos = stream.get_Position(); + n = Math.min(n, Math.max(0, stream.get_Length() - pos)); + stream.set_Position(pos + n); + return n; + } + catch (cli.System.IO.IOException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + catch (cli.System.ObjectDisposedException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + } + else + { + return super.skip(n); + } + } + + public int available() throws java.io.IOException + { + if (stream.get_CanSeek()) + { + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + long val = stream.get_Length() - stream.get_Position(); + return (int)Math.min(Math.max(val, 0), Integer.MAX_VALUE); + } + catch (cli.System.IO.IOException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + catch (cli.System.ObjectDisposedException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + } + else + { + return 0; + } + } + + public void close() throws java.io.IOException + { + stream.Close(); + } + + public void mark(int readlimit) + { + if (stream.get_CanSeek()) + { + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + markPosition = stream.get_Position(); + } + catch (cli.System.IO.IOException x) + { + } + catch (cli.System.ObjectDisposedException x) + { + } + } + } + + public void reset() throws java.io.IOException + { + if (!stream.get_CanSeek()) + { + throw new java.io.IOException("mark/reset not supported"); + } + if (markPosition == -1) + { + throw new java.io.IOException("no mark available"); + } + try + { + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.ObjectDisposedException(null); + stream.set_Position(markPosition); + } + catch (cli.System.IO.IOException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + catch (cli.System.ObjectDisposedException x) + { + java.io.IOException ex = new java.io.IOException(); + ex.initCause(x); + throw ex; + } + } + + public boolean markSupported() + { + return stream.get_CanSeek(); + } +} diff --git a/runtime/classpath.cs b/runtime/classpath.cs index 2614d32a..2bd1e99f 100644 --- a/runtime/classpath.cs +++ b/runtime/classpath.cs @@ -1,5 +1,5 @@ /* - Copyright (C) 2002, 2003, 2004, 2005 Jeroen Frijters + Copyright (C) 2002, 2003, 2004, 2005, 2006 Jeroen Frijters This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -1293,11 +1293,233 @@ namespace IKVM.NativeCode.java namespace IKVM.NativeCode.gnu.java.net.protocol.ikvmres { - public class IkvmresURLConnection + class LZInputStream : Stream { - public static string MangleResourceName(string name) + private Stream inp; + private int[] ptr_tbl; + private int[] char_tbl; + private int[] stack; + private int table_size; + private int count; + private int bitoff; + private int bitbuf; + private int prev = -1; + private int bits; + private int cc; + private int fc; + private int sp; + + public LZInputStream(Stream inp) { - return JVM.MangleResourceName(name); + this.inp = inp; + bitoff = 0; + count = 0; + table_size = 256; + bits = 9; + ptr_tbl = new int[table_size]; + char_tbl = new int[table_size]; + stack = new int[table_size]; + sp = 0; + cc = prev = incode(); + stack[sp++] = cc; + } + + private int read() + { + if (sp == 0) + { + if (stack.Length != table_size) + { + stack = new int[table_size]; + } + int ic = cc = incode(); + if (cc == -1) + { + return -1; + } + if (count >= 0 && cc >= count + 256) + { + stack[sp++] = fc; + cc = prev; + ic = find(prev, fc); + } + while (cc >= 256) + { + stack[sp++] = char_tbl[cc - 256]; + cc = ptr_tbl[cc - 256]; + } + fc = stack[sp++] = cc; + if (count >= 0) + { + ptr_tbl[count] = prev; + char_tbl[count] = fc; + } + count++; + if (count == table_size) + { + count = -1; + if (bits == 12) + { + table_size = 256; + bits = 9; + } + else + { + bits++; + table_size = (1 << bits) - 256; + } + ptr_tbl = null; + char_tbl = null; + ptr_tbl = new int[table_size]; + char_tbl= new int[table_size]; + } + prev = ic; + } + return stack[--sp] & 0xFF; + } + + private int find(int p, int c) + { + int i; + for (i = 0; i < count; i++) + { + if (ptr_tbl[i] == p && char_tbl[i] == c) + { + break; + } + } + return i + 256; + } + + private int incode() + { + while (bitoff < bits) + { + int v = inp.ReadByte(); + if (v == -1) + { + return -1; + } + bitbuf |= (v & 0xFF) << bitoff; + bitoff += 8; + } + bitoff -= bits; + int result = bitbuf; + bitbuf >>= bits; + result -= bitbuf << bits; + return result; + } + + public override int Read(byte[] b, int off, int len) + { + int i = 0; + for (; i < len ; i++) + { + int r = read(); + if(r == -1) + { + break; + } + b[off + i] = (byte)r; + } + return i; + } + + public override bool CanRead + { + get + { + return true; + } + } + + public override bool CanSeek + { + get + { + return false; + } + } + + public override bool CanWrite + { + get + { + return false; + } + } + + public override void Flush() + { + throw new NotSupportedException(); + } + + public override long Length + { + get + { + throw new NotSupportedException(); + } + } + + public override long Position + { + get + { + throw new NotSupportedException(); + } + set + { + throw new NotSupportedException(); + } + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotSupportedException(); + } + + public override void SetLength(long value) + { + throw new NotSupportedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + } + + public class Handler + { + public static Stream ReadResourceFromAssemblyImpl(Assembly asm, string resource) + { + using(Stream s = asm.GetManifestResourceStream(JVM.MangleResourceName(resource.Substring(1)))) + { + if(s == null) + { + throw new FileNotFoundException("resource " + resource + " not found in assembly " + asm.FullName); + } + using(System.Resources.ResourceReader r = new System.Resources.ResourceReader(s)) + { + foreach(DictionaryEntry de in r) + { + if((string)de.Key == "lz") + { + return new LZInputStream(new MemoryStream((byte[])de.Value)); + } + else if((string)de.Key == "ikvm") + { + return new MemoryStream((byte[])de.Value); + } + else + { + throw new IOException("Unsupported resource encoding " + de.Key + " for resource " + resource + " found in assembly " + asm.FullName); + } + } + throw new IOException("Invalid resource " + resource + " found in assembly " + asm.FullName); + } + } } } }