Skip to content

Commit 03e9d7d

Browse files
author
Ian Hopkins
committed
Updates to JDK1.
1 parent 1e87317 commit 03e9d7d

File tree

7 files changed

+352
-31
lines changed

7 files changed

+352
-31
lines changed
+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
#include "Object.h"
22

3+
// public Class java.lang.Object.getClass()
34
JNIEXPORT jobject JNICALL Java_alt_java_lang_Object2_getClass2
4-
(JNIEnv *, jobject) {
5+
(JNIEnv *env, jobject thisObj) {
56

7+
// Ask JNI what class this object is
8+
return (*env)->GetObjectClass(env, thisObj);
9+
}
610

11+
JNIEXPORT jint JNICALL Java_alt_java_lang_Object2_hashCode
12+
(JNIEnv *env, jobject thisObj) {
13+
// Cast the object pointer to an int
14+
// This only works if the garbage collector never moves objects (it does though)
15+
return (jint) thisObj;
716
}
+83-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,95 @@
11
#include "System.h"
2+
#include <sys/time.h>
23

4+
// public static long currentTimeMillis();
35
JNIEXPORT jlong JNICALL Java_alt_java_lang_System_currentTimeMillis
46
(JNIEnv *env, jclass classObj) {
57

8+
// Get a timeval struct of time since EPOCH
9+
struct timeval currentTime;
10+
gettimeofday(&timeval, NULL);
611

12+
// Convert the timeval struct to a jlong milliseconds
13+
jlong rv = ((jlong) currentTime.tv_sec) * 1000;
14+
rv += (jlong) (currentTime.tv_usec / 1000);
15+
return rv;
716
}
817

18+
// public static void arraycopy(Object src, int srcOffset, Object dst, int dstOffset, int length);
919
JNIEXPORT void JNICALL Java_alt_java_lang_System_arraycopy
10-
(JNIEnv *, jclass, jobject, jint, jobject, jint, jint) {
20+
(JNIEnv *env, jclass systemClass, jobject src, jint srcOffset, jobject dst, jint dstOffset, jint length) {
1121

1222

23+
if (dst == null) {
24+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.NullPointerException");
25+
(*env)->ThrowNew(env, toThrowClass, "dst cannot be null");
26+
return;
27+
}
28+
if (src == null) {
29+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.NullPointerException");
30+
(*env)->ThrowNew(env, toThrowClass, "src cannot be null");
31+
return;
32+
}
33+
34+
// Figure out the classes of each array
35+
jclass srcClass = (*env)->GetObjectClass(env, src);
36+
jclass dstClass = (*env)->GetObjectClass(env, dst);
37+
38+
if (srcClass)
39+
40+
jsize srcLength = (*env)->GetArrayLength(env, src);
41+
jsize dstLength = (*env)->GetArrayLength(env, dst);
42+
43+
if (srcOffset < 0) {
44+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.IndexOutOfBoundsException");
45+
(*env)->ThrowNew(env, toThrowClass, "srcOffset cannot be negative");
46+
return;
47+
}
48+
if (dstOffset < 0) {
49+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.IndexOutOfBoundsException");
50+
(*env)->ThrowNew(env, toThrowClass, "dstOffset cannot be negative");
51+
return;
52+
}
53+
if (length < 0) {
54+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.IndexOutOfBoundsException");
55+
(*env)->ThrowNew(env, toThrowClass, "length cannot be negative");
56+
return;
57+
}
58+
if (srcOffset+length > srcLength) {
59+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.IndexOutOfBoundsException");
60+
(*env)->ThrowNew(env, toThrowClass, "srcOffset+length exceeds src.length");
61+
return;
62+
}
63+
if (dstOffset+length > dstLength) {
64+
jclass toThrowClass = (*env)->FindClass(env, "java.lang.IndexOutOfBoundsException");
65+
(*env)->ThrowNew(env, toThrowClass, "dstOffset+length exceeds dst.length");
66+
return;
67+
}
68+
69+
int elementWidth = 4;
70+
/*
71+
switch (type) {
72+
case "jlong":
73+
case "jdouble":
74+
elementWidth = sizeof(jlong);
75+
break;
76+
case "jchar":
77+
case "jshort":
78+
elementWidth = sizeof(jchar);
79+
break
80+
case "jbyte":
81+
case "jboolean":
82+
elementWidth = sizeof(jbyte);
83+
break;
84+
case "jint":
85+
case "jfloat":
86+
case "jobject":
87+
default:
88+
elementWidth = sizeof(jint);
89+
break;
90+
}
91+
*/
92+
93+
memcpy(src, dst, length * elementWidth);
94+
1395
}

src/main/java/alt/java/io/File.java

+40-16
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ public class File {
1111
private String name;
1212

1313
public File(String name) throws NullPointerException {
14-
init(null, name);
14+
this((File) null, name);
1515
}
1616
public File(String dirname, String name) throws NullPointerException {
17-
init(dirname, name);
17+
this(new File(dirname), name);
1818
}
1919
public File(File dir, String name) throws NullPointerException {
20-
init(dir.getName(), name);
21-
}
22-
23-
private void init(String dirname, String name) throws NullPointerException {
2420
if (name == null) {
2521
throw new NullPointerException("name cannot be null");
2622
}
23+
this.parent = dir;
2724
this.name = name;
2825
}
2926

@@ -92,27 +89,29 @@ private void checkDelete() throws SecurityException {
9289

9390
public boolean exists() throws SecurityException {
9491
checkRead();
95-
96-
return false;
92+
return exists0();
9793
}
94+
private native boolean exists0();
9895

9996
public boolean canRead() throws SecurityException {
10097
checkRead();
101-
102-
return false;
98+
return canRead0();
10399
}
100+
private native boolean canRead0();
104101

105102
public boolean isFile() throws SecurityException {
106103
checkRead();
107104

108-
return false;
105+
return isFile0();
109106
}
107+
private native boolean isFile0();
110108

111109
public boolean isDirectory() throws SecurityException {
112110
checkRead();
113111

114-
return false;
112+
return isDirectory0();
115113
}
114+
private native boolean isDirectory0();
116115

117116
public long lastModified() throws SecurityException {
118117
checkRead();
@@ -134,8 +133,15 @@ public boolean mkdir() throws SecurityException {
134133

135134
public boolean mkdirs() throws SecurityException {
136135
checkWrite();
137-
138-
return false;
136+
137+
String parent = getParent();
138+
if (parent != null) {
139+
File parentFile = new File(parent);
140+
if (!parentFile.mkdirs()) {
141+
return false;
142+
}
143+
}
144+
return mkdir();
139145
}
140146

141147
public String[] list() throws SecurityException {
@@ -146,9 +152,27 @@ public String[] list() throws SecurityException {
146152
}
147153

148154
public String[] list(FilenameFilter filter) {
149-
checkRead();
155+
String[] list = list();
150156

151-
return new String[] {};
157+
// How many files match?
158+
int matches = 0;
159+
for(int i = 0; i < list.length; i++) {
160+
if (filter.accept(this, list[i])) {
161+
matches++;
162+
}
163+
}
164+
165+
// Build the array of matches
166+
String[] rv = new String[matches];
167+
int pos = 0;
168+
for(int i = 0; i < list.length; i++) {
169+
if (filter.accept(this, list[i])) {
170+
rv[pos] = list[i];
171+
pos++;
172+
}
173+
}
174+
175+
return rv;
152176
}
153177

154178
public boolean renameTo(File dest) throws SecurityException {

0 commit comments

Comments
 (0)