Skip to content

Commit 6158707

Browse files
committed
URL and File processing improved, better /TEMP/ file handling
1 parent 8071caf commit 6158707

24 files changed

+1010
-815
lines changed

sources/net.sf.j2s.java.core/src/java/io/ByteArrayInputStream.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,33 @@ public synchronized long skip(long n) {
222222
// }
223223
//
224224

225+
226+
227+
/**
228+
* Java 9
229+
*
230+
* @param out
231+
* @return
232+
* @throws IOException
233+
*/
234+
@Override
235+
public long transferTo​(OutputStream out)
236+
throws IOException {
237+
byte[] b = (pos == 0 ? buf : readAllBytes());
238+
out.write(b);
239+
return b.length;
240+
}
241+
242+
/**
243+
* Java 9
244+
*
245+
* @return
246+
* @throws IOException
247+
*/
248+
@Override
249+
public byte[] readAllBytes() throws IOException {
250+
byte[] b = new byte[this.available()];
251+
read(b, 0, b.length);
252+
return b;
253+
}
225254
}

sources/net.sf.j2s.java.core/src/java/io/File.java

Lines changed: 108 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Random;
3838

3939
import swingjs.JSFileSystem.JSPath;
40-
import swingjs.JSTempFile;
4140

4241

4342

@@ -143,6 +142,8 @@ public class File
143142

144143

145144
public byte[] 秘bytes; // filled in by SwingJS ajax call or drag-drop from JSDnD
145+
146+
public boolean 秘isTempFile;
146147
//
147148
// /**
148149
// * The FileSystem object representing the platform's local file system.
@@ -232,34 +233,6 @@ public File(String pathname) {
232233
// this.prefixLength = fs.prefixLength(this.path);
233234
}
234235

235-
/**
236-
* Internal constructor for already-normalized pathname strings.
237-
*/
238-
private File(String pathname, int prefixLength) {
239-
this.path = pathname;
240-
this.prefixLength = prefixLength;
241-
}
242-
243-
/**
244-
* Internal constructor for already-normalized pathname strings.
245-
* The parameter order is used to disambiguate this method from the
246-
* public(File, String) constructor.
247-
*/
248-
private File(String child, File parent) {
249-
assert parent.path != null;
250-
assert (!parent.path.equals(""));
251-
this.path = resolve(parent.path, child);
252-
this.prefixLength = parent.prefixLength;
253-
}
254-
255-
private String resolve(String path, String child) {
256-
if (path == "." && child.startsWith("./"))
257-
return child;
258-
if (child.length() > 0 && !child.startsWith("/") && !path.endsWith("/"))
259-
path += "/";
260-
return path + child;
261-
}
262-
263236
/* Note: The two-argument File constructors do not interpret an empty
264237
parent abstract pathname as the current user directory. An empty parent
265238
instead causes the child to be resolved against the system-dependent
@@ -298,14 +271,12 @@ public File(String parent, String child) {
298271
}
299272
if (parent != null) {
300273
if (parent.equals("") && !child.startsWith("/")) {
301-
this.path = resolve(".", child); // fs.resolve(fs.getDefaultParent(),
302-
// fs.normalize(child));
274+
this.path = resolve(".", fs.normalize(child));
303275
} else {
304-
this.path = resolve(parent, child);// fs.resolve(fs.normalize(parent),
305-
// fs.normalize(child));
276+
this.path = resolve(fs.normalize(parent), fs.normalize(child));
306277
}
307278
} else {
308-
this.path = resolve(".", child);// normalize(child);
279+
this.path = resolve(".", fs.normalize(child));
309280
}
310281
this.prefixLength = this.path.lastIndexOf("/") + 1; // 1efixLength(this.path);
311282
}
@@ -355,76 +326,108 @@ public File(File parent, String child) {
355326
// this.prefixLength = fs.prefixLength(this.path);
356327
}
357328

358-
// /**
359-
// * Creates a new <tt>File</tt> instance by converting the given
360-
// * <tt>file:</tt> URI into an abstract pathname.
361-
// *
362-
// * <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
363-
// * the transformation performed by this constructor is also
364-
// * system-dependent.
365-
// *
366-
// * <p> For a given abstract pathname <i>f</i> it is guaranteed that
367-
// *
368-
// * <blockquote><tt>
369-
// * new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI()
370-
// toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile()
371-
// getAbsoluteFile}())
372-
// * </tt></blockquote>
373-
// *
374-
// * so long as the original abstract pathname, the URI, and the new abstract
375-
// * pathname are all created in (possibly different invocations of) the same
376-
// * Java virtual machine. This relationship typically does not hold,
377-
// * however, when a <tt>file:</tt> URI that is created in a virtual machine
378-
// * on one operating system is converted into an abstract pathname in a
379-
// * virtual machine on a different operating system.
380-
// *
381-
// * @param uri
382-
// * An absolute, hierarchical URI with a scheme equal to
383-
// * <tt>"file"</tt>, a non-empty path component, and undefined
384-
// * authority, query, and fragment components
385-
// *
386-
// * @throws NullPointerException
387-
// * If <tt>uri</tt> is <tt>null</tt>
388-
// *
389-
// * @throws IllegalArgumentException
390-
// * If the preconditions on the parameter do not hold
391-
// *
392-
// * @see #toURI()
393-
// * @see java.net.URI
394-
// * @since 1.4
395-
// */
396-
// public File(URI uri) {
397-
//
398-
// // Check our many preconditions
399-
// if (!uri.isAbsolute())
400-
// throw new IllegalArgumentException("URI is not absolute");
401-
// if (uri.isOpaque())
402-
// throw new IllegalArgumentException("URI is not hierarchical");
403-
// String scheme = uri.getScheme();
404-
// if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
405-
// throw new IllegalArgumentException("URI scheme is not \"file\"");
406-
// if (uri.getAuthority() != null)
407-
// throw new IllegalArgumentException("URI has an authority component");
408-
// if (uri.getFragment() != null)
409-
// throw new IllegalArgumentException("URI has a fragment component");
410-
// if (uri.getQuery() != null)
411-
// throw new IllegalArgumentException("URI has a query component");
412-
// String p = uri.getPath();
413-
// if (p.equals(""))
414-
// throw new IllegalArgumentException("URI path component is empty");
415-
//
416-
// // Okay, now initialize
417-
// p = fs.fromURIPath(p);
418-
// if (File.separatorChar != '/')
419-
// p = p.replace('/', File.separatorChar);
420-
// this.path = fs.normalize(p);
421-
// this.prefixLength = fs.prefixLength(this.path);
422-
// }
423-
//
329+
/**
330+
* Creates a new <tt>File</tt> instance by converting the given <tt>file:</tt>
331+
* URI into an abstract pathname.
332+
*
333+
* <p>
334+
* The exact form of a <tt>file:</tt> URI is system-dependent, hence the
335+
* transformation performed by this constructor is also system-dependent.
336+
*
337+
* <p>
338+
* For a given abstract pathname <i>f</i> it is guaranteed that
339+
*
340+
* <blockquote><tt>
341+
* new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI()
342+
toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile()
343+
getAbsoluteFile}())
344+
* </tt></blockquote>
345+
*
346+
* so long as the original abstract pathname, the URI, and the new abstract
347+
* pathname are all created in (possibly different invocations of) the same Java
348+
* virtual machine. This relationship typically does not hold, however, when a
349+
* <tt>file:</tt> URI that is created in a virtual machine on one operating
350+
* system is converted into an abstract pathname in a virtual machine on a
351+
* different operating system.
352+
*
353+
* @param uri An absolute, hierarchical URI with a scheme equal to
354+
* <tt>"file"</tt>, a non-empty path component, and undefined
355+
* authority, query, and fragment components
356+
*
357+
* @throws NullPointerException If <tt>uri</tt> is <tt>null</tt>
358+
*
359+
* @throws IllegalArgumentException If the preconditions on the parameter do not
360+
* hold
361+
*
362+
* @see #toURI()
363+
* @see java.net.URI
364+
* @since 1.4
365+
*/
366+
public File(URI uri) {
367+
368+
String err = null, scheme, p;
369+
// Check our many preconditions
370+
if (!uri.isAbsolute())
371+
err = ("URI is not absolute");
372+
else if (uri.isOpaque())
373+
err = ("URI is not hierarchical");
374+
else if (((scheme = uri.getScheme()) == null) || !scheme.equalsIgnoreCase("file"))
375+
err = ("URI scheme is not \"file\"");
376+
else if (uri.getAuthority() != null)
377+
err = ("URI has an authority component");
378+
else if (uri.getFragment() != null)
379+
err = ("URI has a fragment component");
380+
else if (uri.getQuery() != null)
381+
err = ("URI has a query component");
382+
else if ((p = uri.getPath()).equals(""))
383+
err = ("URI path component is empty");
384+
else {
385+
// Okay, now initialize
386+
p = fs.fromURIPath(p);
387+
// if (File.separatorChar != '/')
388+
// p = p.replace('/', File.separatorChar);
389+
this.path = //fs.normalize
390+
(p);
391+
this.prefixLength = fs.prefixLength(this.path);
392+
return;
393+
}
394+
throw new IllegalArgumentException(err);
395+
}
396+
424397

425398
/* -- Path-component accessors -- */
426399

427-
/**
400+
/**
401+
* Internal constructor for already-normalized pathname strings.
402+
*/
403+
private File(String pathname, int prefixLength) {
404+
this.path = pathname;
405+
this.prefixLength = prefixLength;
406+
}
407+
408+
/**
409+
* Internal constructor for already-normalized pathname strings.
410+
* The parameter order is used to disambiguate this method from the
411+
* public(File, String) constructor.
412+
*/
413+
private File(String child, File parent) {
414+
// assert parent.path != null;
415+
// assert (!parent.path.equals(""));
416+
this.path = resolve(parent.path, child);
417+
this.prefixLength = parent.prefixLength;
418+
}
419+
420+
private String resolve(String path, String child) {
421+
if (path == "." && child.startsWith("./"))
422+
return child;
423+
if (child.length() > 0 && !child.startsWith("/") && !path.endsWith("/"))
424+
path += "/";
425+
path = path + child;
426+
this.秘isTempFile = path.startsWith(temporaryDirectory);
427+
return path;
428+
}
429+
430+
/**
428431
* Returns the name of the file or directory denoted by this abstract
429432
* pathname. This is just the last name in the pathname's name
430433
* sequence. If the pathname's name sequence is empty, then the empty
@@ -891,13 +894,7 @@ public boolean isFile() {
891894
* method denies read access to the file
892895
*/
893896
public long length() {
894-
// SecurityManager security = System.getSecurityManager();
895-
// if (security != null) {
896-
// security.checkRead(path);
897-
// }
898-
// return fs.getLength(this);
899-
return (/** @j2sNative this.秘bytes ? this.秘bytes.length : */0);
900-
897+
return fs.getLength(this);
901898
}
902899

903900

@@ -1752,7 +1749,9 @@ private static File generateFile(String prefix, String suffix, File dir)
17521749
} else {
17531750
n = Math.abs(n);
17541751
}
1755-
return new JSTempFile(dir, prefix + Long.toString(n) + suffix);
1752+
File f = new File(dir, prefix + Long.toString(n) + suffix);
1753+
f.秘isTempFile = true;
1754+
return f;
17561755
}
17571756
//
17581757
// private static boolean checkAndCreate(String filename, SecurityManager sm,

sources/net.sf.j2s.java.core/src/java/io/FileDescriptor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.util.List;
3333
import java.util.concurrent.atomic.AtomicInteger;
3434

35-
import swingjs.JSTempFile;
36-
3735
/**
3836
* Instances of the file descriptor class serve as an opaque handle
3937
* to the underlying machine-specific structure representing an open
@@ -237,7 +235,7 @@ int decrementAndGetUseCount() {
237235
synchronized void attach(Closeable c) {
238236

239237
_file = (/** @j2sNative c._file || */null);
240-
isTempFile = _file instanceof JSTempFile;
238+
isTempFile = _file != null && _file.秘isTempFile;
241239

242240
if (parent == null) {
243241
// first caller gets to do this

sources/net.sf.j2s.java.core/src/java/io/FileInputStream.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525

2626
package java.io;
2727

28-
import java.nio.channels.FileChannel;
29-
//import sun.nio.ch.FileChannelImpl;
30-
3128
import swingjs.JSFileSystem.JSFileChannel;
32-
import swingjs.JSTempFile;
3329
import swingjs.JSUtil;
3430

3531

@@ -61,10 +57,7 @@ class FileInputStream extends InputStream
6157
* (null if the stream is created with a file descriptor)
6258
*/
6359
private final String path;
64-
6560
private JSFileChannel channel = null;
66-
67-
// private final Object closeLock = new Object();
6861
private volatile boolean closed = false;
6962

7063
public File _file;
@@ -97,9 +90,7 @@ class FileInputStream extends InputStream
9790
* @see java.lang.SecurityManager#checkRead(java.lang.String)
9891
*/
9992
public FileInputStream(String name) throws FileNotFoundException {
100-
this((File) (name == null ? null
101-
: name.startsWith(File.temporaryDirectory) ? new JSTempFile(name)
102-
: new File(name)));
93+
this((File) (name == null ? null : new File(name)));
10394
}
10495

10596
/**
@@ -427,4 +418,39 @@ protected void finalize() throws IOException {
427418
close();
428419
}
429420
}
421+
/**
422+
* Java 9
423+
*
424+
* @param out
425+
* @return
426+
* @throws IOException
427+
*/
428+
@Override
429+
public long transferTo​(OutputStream out)
430+
throws IOException {
431+
if (channel != null) {
432+
return super.transferTo​(out);
433+
}
434+
byte[] b = (is.pos == 0 ? is.buf : is.readAllBytes());
435+
out.write(b);
436+
return b.length;
437+
}
438+
439+
/**
440+
* Java 9
441+
*
442+
* @return
443+
* @throws IOException
444+
*/
445+
@Override
446+
public byte[] readAllBytes() throws IOException {
447+
if (channel != null)
448+
super.readAllBytes();
449+
if (is.pos == 0)
450+
return is.buf;
451+
byte[] b = new byte[available()];
452+
read(b, 0, b.length);
453+
return b;
454+
}
455+
430456
}

0 commit comments

Comments
 (0)