Skip to content

Commit

Permalink
Found Potential Fix for javapathfinder#429
Browse files Browse the repository at this point in the history
Seems like there needs to be model classes in the file and charset packages for java.nio. These are as small as I could make them
  • Loading branch information
joalen committed Nov 17, 2024
1 parent 8fe399b commit d48e34d
Show file tree
Hide file tree
Showing 9 changed files with 398 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/classes/modules/java.base/java/nio/charset/Charset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package java.nio.charset;

import java.util.HashMap;
import java.util.Map;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class Charset {
private static final String DEFAULT_CHARSET_NAME = "UTF-8";
private static final Map<String, Charset> charsetMap = new HashMap<>();
private final String name;

// Static initialization to ensure default charset always exists
static {
Charset defaultCharset = new Charset(DEFAULT_CHARSET_NAME);
charsetMap.put(DEFAULT_CHARSET_NAME.toLowerCase(), defaultCharset);
charsetMap.put("default", defaultCharset);
}

protected Charset(String canonicalName) {
if (canonicalName == null) {
throw new IllegalArgumentException("Charset name cannot be null");
}
this.name = canonicalName;
}

public static Charset defaultCharset() {
return charsetMap.get(DEFAULT_CHARSET_NAME.toLowerCase());
}

public static Charset forName(String charsetName) {
if (charsetName == null) {
throw new IllegalArgumentException("Null charset name");
}

String normalizedName = charsetName.toLowerCase();

// Always return the default charset if the requested one isn't found
// This simplifies the model while preventing null charset issues
return charsetMap.getOrDefault(normalizedName, defaultCharset());
}

public final String name() {
return name;
}

// Simple encode/decode stubs that don't actually do encoding
// but prevent null pointer issues
public CharBuffer decode(ByteBuffer bb) {
if (bb == null) {
throw new IllegalArgumentException("ByteBuffer cannot be null");
}
// Simplified implementation that just copies bytes to chars
char[] chars = new char[bb.remaining()];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) (bb.get() & 0xFF);
}
return CharBuffer.wrap(new String(chars));
}

public ByteBuffer encode(CharBuffer cb) {
if (cb == null) {
throw new IllegalArgumentException("CharBuffer cannot be null");
}
// Simplified implementation that just copies chars to bytes
byte[] bytes = new byte[cb.remaining()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) cb.get();
}
return ByteBuffer.wrap(bytes);
}
}
11 changes: 11 additions & 0 deletions src/classes/modules/java.base/java/nio/files/FileSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package java.nio.file;

import java.nio.file.spi.FileSystemProvider;

public abstract class FileSystem {
protected FileSystem() {}

public abstract Path getPath(String first, String... more);

public abstract FileSystemProvider provider();
}
186 changes: 186 additions & 0 deletions src/classes/modules/java.base/java/nio/files/FileSystems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package java.nio.file;

import java.nio.file.spi.FileSystemProvider;
import java.net.URI;
import java.util.Map;
import java.util.HashMap;

public class FileSystems {
private static volatile FileSystem defaultFileSystem;

static {
defaultFileSystem = new FileSystem() {
private final FileSystemProvider provider = new FileSystemProvider() {
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env) {
return this.getFileSystem();
}

@Override
public String getScheme() {
return "file";
}

@Override
public FileSystem getFileSystem() {
return defaultFileSystem;
}
};

@Override
public Path getPath(String first, String... more) {
return new Path() {
private final String path;
private final String[] segments;

{
StringBuilder sb = new StringBuilder(first);
if (more != null) {
for (String s : more) {
if (s != null && !s.isEmpty()) {
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') {
sb.append('/');
}
sb.append(s);
}
}
}
path = sb.toString();
segments = path.split("/");
}

@Override
public FileSystem getFileSystem() {
return defaultFileSystem;
}

@Override
public boolean isAbsolute() {
return path.startsWith("/");
}

@Override
public Path getRoot() {
return isAbsolute() ? getFileSystem().getPath("/") : null;
}

@Override
public Path getFileName() {
if (path.isEmpty()) return null;
return getFileSystem().getPath(segments[segments.length - 1]);
}

@Override
public Path getParent() {
int lastSep = path.lastIndexOf('/');
return lastSep > 0 ? getFileSystem().getPath(path.substring(0, lastSep)) : null;
}

@Override
public int getNameCount() {
if (path.isEmpty()) return 0;
if (path.equals("/")) return 0;
int count = segments.length;
if (path.startsWith("/")) count--;
if (path.endsWith("/")) count--;
return count;
}

@Override
public Path getName(int index) {
if (index < 0 || index >= getNameCount()) {
throw new IllegalArgumentException();
}
return getFileSystem().getPath(segments[path.startsWith("/") ? index + 1 : index]);
}

@Override
public Path subpath(int beginIndex, int endIndex) {
if (beginIndex < 0 || beginIndex >= getNameCount() ||
endIndex > getNameCount() || beginIndex >= endIndex) {
throw new IllegalArgumentException();
}

StringBuilder result = new StringBuilder();
int start = path.startsWith("/") ? beginIndex + 1 : beginIndex;
for (int i = start; i < start + (endIndex - beginIndex); i++) {
if (result.length() > 0) result.append('/');
result.append(segments[i]);
}
return getFileSystem().getPath(result.toString());
}

@Override
public boolean startsWith(Path other) {
return path.startsWith(other.toString());
}

@Override
public boolean endsWith(Path other) {
return path.endsWith(other.toString());
}

@Override
public Path normalize() {
return this; // Simplified implementation
}

@Override
public Path resolve(Path other) {
return getFileSystem().getPath(path + "/" + other.toString());
}

@Override
public Path relativize(Path other) {
return other; // Simplified implementation
}

@Override
public URI toUri() {
try {
return new URI("file", null, path, null);
} catch (Exception e) {
return null;
}
}

@Override
public Path toAbsolutePath() {
if (isAbsolute()) return this;
return getFileSystem().getPath("/" + path);
}

@Override
public Path toRealPath(LinkOption... options) {
return toAbsolutePath();
}

@Override
public String toString() {
return path;
}

@Override
public int compareTo(Path other) {
return path.compareTo(other.toString());
}
};
}

@Override
public FileSystemProvider provider() {
return provider;
}
};
}

public static FileSystem getDefault() {
return defaultFileSystem;
}

public static FileSystem newFileSystem(URI uri, Map<String, ?> env, ClassLoader loader) {
if (uri == null)
throw new NullPointerException();
return defaultFileSystem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package java.nio.file;

public enum FileVisitOption {
FOLLOW_LINKS
}
50 changes: 50 additions & 0 deletions src/classes/modules/java.base/java/nio/files/Files.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package java.nio.file;

import java.nio.file.attribute.*;
import java.nio.file.spi.FileSystemProvider;
import java.util.Set;
import java.util.stream.Stream;
import java.io.IOException;

public class Files {
private Files() {}

public static FileSystem getFileSystem(Path path) {
return path.getFileSystem();
}

public static FileSystemProvider provider(Path path) {
return path.getFileSystem().provider();
}

public static <A extends BasicFileAttributes> A readAttributes(
Path path, Class<A> type, LinkOption... options) throws IOException {
return (A) new BasicFileAttributes() {
public FileTime lastModifiedTime() { return FileTime.fromMillis(System.currentTimeMillis()); }
public FileTime lastAccessTime() { return FileTime.fromMillis(System.currentTimeMillis()); }
public FileTime creationTime() { return FileTime.fromMillis(System.currentTimeMillis()); }
public boolean isRegularFile() { return true; }
public boolean isDirectory() { return false; }
public boolean isSymbolicLink() { return false; }
public boolean isOther() { return false; }
public long size() { return 0L; }
public Object fileKey() { return null; }
};
}

public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException {
return Stream.of(start);
}

public static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException {
return Stream.of(start);
}

public static boolean exists(Path path) throws IOException {
try {
return Files.exists(path);
} catch (IOException e) {
return false;
}
}
}
25 changes: 25 additions & 0 deletions src/classes/modules/java.base/java/nio/files/Path.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package java.nio.file;
import java.net.URI;

public interface Path extends Comparable<Path> {
FileSystem getFileSystem();
boolean isAbsolute();
Path getRoot();
Path getFileName();
Path getParent();
int getNameCount();
Path getName(int index);
Path subpath(int beginIndex, int endIndex);
boolean startsWith(Path other);
boolean endsWith(Path other);
Path normalize();
Path resolve(Path other);
Path relativize(Path other);
URI toUri();
Path toAbsolutePath();
Path toRealPath(LinkOption... options);

static Path of(String first, String... more) {
return FileSystems.getDefault().getPath(first, more);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package java.nio.file.attribute;

public interface BasicFileAttributes {
FileTime lastModifiedTime();
FileTime lastAccessTime();
FileTime creationTime();
boolean isRegularFile();
boolean isDirectory();
boolean isSymbolicLink();
boolean isOther();
long size();
Object fileKey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package java.nio.file.attribute;

public final class FileTime {
private final long time;

private FileTime(long time) {
this.time = time;
}

public static FileTime fromMillis(long time) {
return new FileTime(time);
}

public long toMillis() {
return time;
}
}
Loading

0 comments on commit d48e34d

Please sign in to comment.