Merge remote-tracking branch 'rt/master'

This commit is contained in:
Luke Wahlmeier
2016-01-10 03:04:30 -07:00
34 changed files with 1309 additions and 650 deletions

View File

@ -7,9 +7,11 @@
There is NO WARRANTY for this software. See license.txt for
details. */
package java.io;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStream extends InputStream {
private final InputStream in;
private final byte[] buffer;
@ -25,17 +27,16 @@ public class BufferedInputStream extends InputStream {
this(in, 4096);
}
private void fill() throws IOException {
private int fill() throws IOException {
position = 0;
limit = in.read(buffer);
return limit;
}
public int read() throws IOException {
if (position >= limit) {
fill();
if (limit == -1) {
return -1;
}
if (position >= limit && fill() == -1) {
return -1;
}
return buffer[position++] & 0xFF;
@ -43,7 +44,9 @@ public class BufferedInputStream extends InputStream {
public int read(byte[] b, int offset, int length) throws IOException {
int count = 0;
if (position >= limit && fill() == -1) {
return -1;
}
if (position < limit) {
int remaining = limit - position;
if (remaining > length) {
@ -57,8 +60,8 @@ public class BufferedInputStream extends InputStream {
offset += remaining;
length -= remaining;
}
while (length > 0) {
while (length > 0 && in.available() > 0)
{
int c = in.read(b, offset, length);
if (c == -1) {
if (count == 0) {
@ -69,13 +72,8 @@ public class BufferedInputStream extends InputStream {
offset += c;
count += c;
length -= c;
if (in.available() <= 0) {
break;
}
}
}
return count;
}
@ -87,3 +85,4 @@ public class BufferedInputStream extends InputStream {
in.close();
}
}

View File

@ -15,7 +15,10 @@ import java.util.Random;
public final class Math {
public static final double E = 2.718281828459045;
public static final double PI = 3.141592653589793;
private static final Random random = new Random();
private static class Static {
public static final Random random = new Random();
}
private Math() { }
@ -84,7 +87,7 @@ public final class Math {
}
public static double random() {
return random.nextDouble();
return Static.random.nextDouble();
}
public static native double floor(double v);

View File

@ -22,7 +22,9 @@ import java.util.Hashtable;
import java.util.Properties;
public abstract class System {
private static final long NanoTimeBaseInMillis = currentTimeMillis();
private static class NanoTime {
public static final long BaseInMillis = currentTimeMillis();
}
private static class Static {
public static Properties properties = makeProperties();
@ -94,7 +96,7 @@ public abstract class System {
public static native int identityHashCode(Object o);
public static long nanoTime() {
return (currentTimeMillis() - NanoTimeBaseInMillis) * 1000000;
return (currentTimeMillis() - NanoTime.BaseInMillis) * 1000000;
}
public static String mapLibraryName(String name) {

View File

@ -187,14 +187,28 @@ public class LambdaMetafactory {
return result;
}
public static CallSite metafactory(MethodHandles.Lookup caller,
String invokedName,
MethodType invokedType,
MethodType methodType,
MethodHandle methodImplementation,
MethodType instantiatedMethodType)
throws LambdaConversionException
public static byte[] makeLambda(String invokedName,
String invokedType,
String methodType,
String implementationClass,
String implementationName,
String implementationSpec,
int implementationKind)
{
return makeLambda(invokedName,
new MethodType(invokedType),
new MethodType(methodType),
new MethodHandle(implementationClass,
implementationName,
implementationSpec,
implementationKind));
}
private static byte[] makeLambda(String invokedName,
MethodType invokedType,
MethodType methodType,
MethodHandle methodImplementation)
{
String className;
{ int number;
@ -265,8 +279,19 @@ public class LambdaMetafactory {
throw error;
}
byte[] classData = out.toByteArray();
return out.toByteArray();
}
public static CallSite metafactory(MethodHandles.Lookup caller,
String invokedName,
MethodType invokedType,
MethodType methodType,
MethodHandle methodImplementation,
MethodType instantiatedMethodType)
throws LambdaConversionException
{
byte[] classData = makeLambda(invokedName, invokedType, methodType, methodImplementation);
try {
return new CallSite
(new MethodHandle

View File

@ -1,6 +1,7 @@
package java.lang.invoke;
import avian.Classes;
import avian.SystemClassLoader;
public class MethodHandle {
static final int REF_invokeStatic = 6;
@ -17,6 +18,20 @@ public class MethodHandle {
this.method = method;
}
MethodHandle(String class_,
String name,
String spec,
int kind)
{
this.kind = kind;
this.loader = SystemClassLoader.appLoader();
try {
this.method = Classes.findMethod(this.loader, class_, name, spec);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (method.class_ != null) {

View File

@ -4,6 +4,7 @@ import static avian.Assembler.*;
import avian.Assembler;
import avian.Classes;
import avian.SystemClassLoader;
import avian.VMClass;
import java.util.List;
@ -25,6 +26,12 @@ public final class MethodType implements java.io.Serializable {
this.spec = spec;
}
MethodType(String spec) {
this.loader = SystemClassLoader.appLoader();
this.spec = new byte[spec.length() + 1];
spec.getBytes(0, spec.length(), this.spec, 0);
}
public String toMethodDescriptorString() {
return Classes.makeString(spec, 0, spec.length - 1);
}

View File

@ -86,18 +86,16 @@ public class Socket implements Closeable, AutoCloseable {
@Override
public int read(byte[] buffer) throws IOException {
if(buffer.length == 0) return 0; //spec says return 0 if buffer length is zero.
int fullSize = buffer.length;
int index = 0;
int size;
do {
size = recv(sock, buffer, index, Math.min(fullSize, Socket.BUFFER_SIZE));
fullSize -= size;
index += size;
} while (fullSize != 0 && size != 0);
return index;
size = recv(sock, buffer, 0, Math.min(fullSize, Socket.BUFFER_SIZE));
fullSize -= size;
//removed loop, because otherwise interactive protocols will not work.
if(size < 0) throw new IOException("Error while reading stream"); //as the manpage of recv says, a value below zero indicates an error.
if(size == 0) return -1; // if the stream is closed (size == 0), then return -1 to indicate end of stream.
return size;
}
}
private class SocketOutputStream extends OutputStream {