From 6efade1b4986fb2e3b4628708101e5ee7de33680 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Thu, 21 Sep 2023 08:20:24 +0100 Subject: [PATCH] classlib: Add nio's channel interfaces (#751) - Add the basic java.nio.channels.Channel interfaces. - Add several nio exception types. - Add UncheckedIOException --- .../java/io/TUncheckedIOException.java | 15 ++++++ .../java/nio/channels/TByteChannel.java | 4 ++ .../classlib/java/nio/channels/TChannel.java | 11 +++++ .../nio/channels/TClosedChannelException.java | 7 +++ .../TNonReadableChannelException.java | 5 ++ .../TNonWritableChannelException.java | 5 ++ .../nio/channels/TReadableByteChannel.java | 9 ++++ .../nio/channels/TSeekableByteChannel.java | 13 +++++ .../nio/channels/TWritableByteChannel.java | 9 ++++ .../java/nio/file/TAccessDeniedException.java | 13 +++++ .../java/nio/file/TFileSystemException.java | 47 +++++++++++++++++++ 11 files changed, 138 insertions(+) create mode 100644 classlib/src/main/java/org/teavm/classlib/java/io/TUncheckedIOException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TByteChannel.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TChannel.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TClosedChannelException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonReadableChannelException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonWritableChannelException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TReadableByteChannel.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TSeekableByteChannel.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/channels/TWritableByteChannel.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/file/TAccessDeniedException.java create mode 100644 classlib/src/main/java/org/teavm/classlib/java/nio/file/TFileSystemException.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/io/TUncheckedIOException.java b/classlib/src/main/java/org/teavm/classlib/java/io/TUncheckedIOException.java new file mode 100644 index 000000000..3b32f02b3 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/io/TUncheckedIOException.java @@ -0,0 +1,15 @@ +package org.teavm.classlib.java.io; + +import java.io.IOException; + +public class TUncheckedIOException extends RuntimeException { + private static final long serialVersionUID = 1645785175445590213L; + + public TUncheckedIOException(IOException cause) { + super(cause); + } + + public TUncheckedIOException(String message, IOException cause) { + super(message, cause); + } +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TByteChannel.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TByteChannel.java new file mode 100644 index 000000000..c8f1231bd --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TByteChannel.java @@ -0,0 +1,4 @@ +package org.teavm.classlib.java.nio.channels; + +public interface TByteChannel extends TReadableByteChannel, TWritableByteChannel { +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TChannel.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TChannel.java new file mode 100644 index 000000000..484b59bf5 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TChannel.java @@ -0,0 +1,11 @@ +package org.teavm.classlib.java.nio.channels; + +import org.teavm.classlib.java.io.TCloseable; + +import java.io.IOException; + +public interface TChannel extends TCloseable { + boolean isOpen(); + + void close() throws IOException; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TClosedChannelException.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TClosedChannelException.java new file mode 100644 index 000000000..891d00a0d --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TClosedChannelException.java @@ -0,0 +1,7 @@ +package org.teavm.classlib.java.nio.channels; + +import java.io.IOException; + +public class TClosedChannelException extends IOException { + private static final long serialVersionUID = 5928119801967419918L; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonReadableChannelException.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonReadableChannelException.java new file mode 100644 index 000000000..46d821740 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonReadableChannelException.java @@ -0,0 +1,5 @@ +package org.teavm.classlib.java.nio.channels; + +public class TNonReadableChannelException extends IllegalStateException { + private static final long serialVersionUID = -2344456823086925506L; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonWritableChannelException.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonWritableChannelException.java new file mode 100644 index 000000000..62a5117fb --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TNonWritableChannelException.java @@ -0,0 +1,5 @@ +package org.teavm.classlib.java.nio.channels; + +public class TNonWritableChannelException extends IllegalStateException { + private static final long serialVersionUID = -837317372114923332L; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TReadableByteChannel.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TReadableByteChannel.java new file mode 100644 index 000000000..5e697f7f4 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TReadableByteChannel.java @@ -0,0 +1,9 @@ +package org.teavm.classlib.java.nio.channels; + +import org.teavm.classlib.java.nio.TByteBuffer; + +import java.io.IOException; + +public interface TReadableByteChannel extends TChannel { + int read(TByteBuffer dst) throws IOException; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TSeekableByteChannel.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TSeekableByteChannel.java new file mode 100644 index 000000000..dfaa88898 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TSeekableByteChannel.java @@ -0,0 +1,13 @@ +package org.teavm.classlib.java.nio.channels; + +import java.io.IOException; + +public interface TSeekableByteChannel extends TByteChannel { + long position(); + + TSeekableByteChannel position(long newPosition) throws IOException; + + long size() throws IOException; + + TSeekableByteChannel truncate(long size) throws IOException; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TWritableByteChannel.java b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TWritableByteChannel.java new file mode 100644 index 000000000..4b601f437 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/channels/TWritableByteChannel.java @@ -0,0 +1,9 @@ +package org.teavm.classlib.java.nio.channels; + +import org.teavm.classlib.java.nio.TByteBuffer; + +import java.io.IOException; + +public interface TWritableByteChannel extends TChannel { + int write(TByteBuffer src) throws IOException; +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/file/TAccessDeniedException.java b/classlib/src/main/java/org/teavm/classlib/java/nio/file/TAccessDeniedException.java new file mode 100644 index 000000000..0d7aef002 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/file/TAccessDeniedException.java @@ -0,0 +1,13 @@ +package org.teavm.classlib.java.nio.file; + +public class TAccessDeniedException extends TFileSystemException { + private static final long serialVersionUID = 6428707745039365878L; + + public TAccessDeniedException(String file) { + super(file); + } + + public TAccessDeniedException(String file, String other, String reason) { + super(file, other, reason); + } +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/file/TFileSystemException.java b/classlib/src/main/java/org/teavm/classlib/java/nio/file/TFileSystemException.java new file mode 100644 index 000000000..61876a652 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/file/TFileSystemException.java @@ -0,0 +1,47 @@ +package org.teavm.classlib.java.nio.file; + +import java.io.IOException; + +public class TFileSystemException extends IOException { + private static final long serialVersionUID = 9093298181737952280L; + + private final String file; + private final String other; + private final String reason; + + public TFileSystemException(String file) { + this.file = file; + this.other = null; + this.reason = null; + } + + public TFileSystemException(String file, String other, String reason) { + super(reason); + this.file = file; + this.other = other; + this.reason = reason; + } + + public String getFile() { + return file; + } + + public String getOtherFile() { + return other; + } + + public String getReason() { + return reason; + } + + @Override + public String getMessage() { + if (file == null && other == null) return reason; + + StringBuilder out = new StringBuilder(); + if (file != null) out.append(file); + if (other != null) out.append(" -> ").append(other); + if (reason != null) out.append(": ").append(reason); + return out.toString(); + } +}