mirror of
https://github.com/corda/corda.git
synced 2025-01-09 14:33:30 +00:00
cddea7187d
Whereas the GNU Classpath port used the strategy of patching Classpath with core classes from Avian so as to minimize changes to the VM, this port uses the opposite strategy: abstract and isolate classpath-specific features in the VM similar to how we abstract away platform-specific features in system.h. This allows us to use an unmodified copy of OpenJDK's class library, including its core classes and augmented by a few VM-specific classes in the "avian" package.
106 lines
2.6 KiB
Java
106 lines
2.6 KiB
Java
/* 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());
|
|
}
|
|
|
|
public void connect() {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|