From 61ba2d617ef01fa9e44fa19deca09fc2821f3428 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 6 Jun 2009 20:38:02 -0600 Subject: [PATCH] meant to include this in the last commit --- classpath/avian/resource/Handler.java | 101 ++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 classpath/avian/resource/Handler.java diff --git a/classpath/avian/resource/Handler.java b/classpath/avian/resource/Handler.java new file mode 100644 index 0000000000..a10e575a85 --- /dev/null +++ b/classpath/avian/resource/Handler.java @@ -0,0 +1,101 @@ +/* Copyright (c) 2008-2009, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package avian.resource; + +import java.net.URL; +import java.net.URLStreamHandler; +import java.net.URLConnection; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.io.InputStream; + +public class Handler extends URLStreamHandler { + protected URLConnection openConnection(URL url) { + return new ResourceConnection(url); + } + + private static class ResourceConnection extends URLConnection { + public ResourceConnection(URL url) { + super(url); + } + + public int getContentLength() { + return ResourceInputStream.getContentLength(url.getFile()); + } + + public InputStream getInputStream() throws IOException { + return new ResourceInputStream(url.getFile()); + } + } + + private static class ResourceInputStream extends InputStream { + private long peer; + private int position; + + public ResourceInputStream(String path) throws IOException { + peer = open(path); + if (peer == 0) { + throw new FileNotFoundException(path); + } + } + + private static native int getContentLength(String path); + + private static native long open(String path) throws IOException; + + private static native int read(long peer, int position) throws IOException; + + private static native int read(long peer, int position, + byte[] b, int offset, int length) + throws IOException; + + public static native void close(long peer) throws IOException; + + public int read() throws IOException { + if (peer != 0) { + int c = read(peer, position); + if (c >= 0) { + ++ position; + } + return c; + } else { + throw new IOException(); + } + } + + public int read(byte[] b, int offset, int length) throws IOException { + if (peer != 0) { + if (b == null) { + throw new NullPointerException(); + } + + if (offset < 0 || offset + length > b.length) { + throw new ArrayIndexOutOfBoundsException(); + } + + int c = read(peer, position, b, offset, length); + if (c >= 0) { + position += c; + } + return c; + } else { + throw new IOException(); + } + } + + public void close() throws IOException { + if (peer != 0) { + close(peer); + peer = 0; + } + } + } +}