Skip to content

Commit e344808

Browse files
committed
passes bytes through to URI and Files.readAllBytes
1 parent 0379355 commit e344808

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,12 @@ private static String slashify(String path, boolean isDirectory) {
701701
// */
702702
public URI toURI() {
703703
try {
704-
File f = getAbsoluteFile();
705-
String sp = slashify(f.getPath(), f.isDirectory());
704+
String sp = slashify(getAbsoluteFile().getPath(), false);
706705
if (sp.startsWith("//"))
707706
sp = "//" + sp;
708-
return new URI("file", null, sp, null);
707+
URI uri = new URI("file", null, sp, null);
708+
uri.秘bytes = 秘bytes;
709+
return uri;
709710
} catch (URISyntaxException x) {
710711
throw new Error(x); // Can't happen
711712
}

sources/net.sf.j2s.java.core/src/java/net/URI.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ public final class URI
506506
private volatile String string; // The only serializable field
507507

508508

509+
/**
510+
* Possibly transmitted from File object
511+
*/
512+
public byte[] 秘bytes;
513+
514+
509515

510516
// -- Constructors and factories --
511517

sources/net.sf.j2s.java.core/src/java/nio/file/Files.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
import java.util.stream.Stream;
7878
import java.util.stream.StreamSupport;
7979

80+
import swingjs.JSFileSystem.JSPath;
81+
8082
/**
8183
* This class consists exclusively of static methods that operate on files,
8284
* directories, or other types of files.
@@ -3125,40 +3127,41 @@ private static byte[] read(InputStream source, int initialSize) throws IOExcepti
31253127
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
31263128
}
31273129

3128-
/**
3129-
* Reads all the bytes from a file. The method ensures that the file is
3130-
* closed when all bytes have been read or an I/O error, or other runtime
3131-
* exception, is thrown.
3132-
*
3133-
* <p> Note that this method is intended for simple cases where it is
3134-
* convenient to read all bytes into a byte array. It is not intended for
3135-
* reading in large files.
3136-
*
3137-
* @param path
3138-
* the path to the file
3139-
*
3140-
* @return a byte array containing the bytes read from the file
3141-
*
3142-
* @throws IOException
3143-
* if an I/O error occurs reading from the stream
3144-
* @throws OutOfMemoryError
3145-
* if an array of the required size cannot be allocated, for
3146-
* example the file is larger that {@code 2GB}
3147-
* @throws SecurityException
3148-
* In the case of the default provider, and a security manager is
3149-
* installed, the {@link SecurityManager#checkRead(String) checkRead}
3150-
* method is invoked to check read access to the file.
3151-
*/
3152-
public static byte[] readAllBytes(Path path) throws IOException {
3153-
try (SeekableByteChannel sbc = Files.newByteChannel(path);
3154-
InputStream in = Channels.newInputStream(sbc)) {
3155-
long size = sbc.size();
3156-
if (size > (long)MAX_BUFFER_SIZE)
3157-
throw new OutOfMemoryError("Required array size too large");
3158-
3159-
return read(in, (int)size);
3160-
}
3161-
}
3130+
/**
3131+
* Reads all the bytes from a file. The method ensures that the file is closed
3132+
* when all bytes have been read or an I/O error, or other runtime exception, is
3133+
* thrown.
3134+
*
3135+
* <p>
3136+
* Note that this method is intended for simple cases where it is convenient to
3137+
* read all bytes into a byte array. It is not intended for reading in large
3138+
* files.
3139+
*
3140+
* @param path the path to the file
3141+
*
3142+
* @return a byte array containing the bytes read from the file
3143+
*
3144+
* @throws IOException if an I/O error occurs reading from the stream
3145+
* @throws OutOfMemoryError if an array of the required size cannot be
3146+
* allocated, for example the file is larger that
3147+
* {@code 2GB}
3148+
* @throws SecurityException In the case of the default provider, and a security
3149+
* manager is installed, the
3150+
* {@link SecurityManager#checkRead(String) checkRead}
3151+
* method is invoked to check read access to the file.
3152+
*/
3153+
public static byte[] readAllBytes(Path path) throws IOException {
3154+
byte[] bytes = ((JSPath) path).秘bytes;
3155+
if (bytes == null) {
3156+
try (SeekableByteChannel sbc = Files.newByteChannel(path); InputStream in = Channels.newInputStream(sbc)) {
3157+
long size = sbc.size();
3158+
if (size > (long) MAX_BUFFER_SIZE) // 2.1 GB; no reason that could not be higher?
3159+
throw new OutOfMemoryError("Required array size too large");
3160+
((JSPath) path).秘bytes = bytes = read(in, (int) size);
3161+
}
3162+
}
3163+
return bytes;
3164+
}
31623165

31633166
/**
31643167
* Read all lines from a file. This method ensures that the file is

sources/net.sf.j2s.java.core/src/swingjs/JSFileSystem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,9 @@ public FileSystem getFileSystem(URI uri) {
933933

934934
@Override
935935
public Path getPath(URI uri) {
936-
return getFileSystem(uri).getPath(uri.toString());
936+
JSPath p = (JSPath) getFileSystem(uri).getPath(uri.toString());
937+
p.秘bytes = uri.秘bytes;
938+
return p;
937939
}
938940

939941
@Override

0 commit comments

Comments
 (0)