From 044d2b2b0641581fd662750b6a064cc5c994be66 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Tue, 28 Jan 2020 22:23:43 +0000 Subject: [PATCH] Some API cleanup - Remove *Stream methods on IMount/IWritableMount, and make the channel ones the primary. - Fix location of AbstractTurtleUpgrade - Make IComputerAccess.getAvailablePeripheral and .getMainThreadMonitor mandatory. - IComputerAccess throws a specialised NotAttachedException --- .../computercraft/api/ComputerCraftAPI.java | 25 --------- .../computercraft/api/filesystem/IMount.java | 23 ++------ .../api/filesystem/IWritableMount.java | 35 +----------- .../computercraft/api/media/IMedia.java | 2 +- .../api/peripheral/IComputerAccess.java | 44 +++++++-------- .../api/peripheral/NotAttachedException.java | 25 +++++++++ .../{ => turtle}/AbstractTurtleUpgrade.java | 4 +- .../core/apis/ComputerAccess.java | 3 +- .../core/apis/PeripheralAPI.java | 53 +++++++------------ .../core/computer/ComputerSystem.java | 18 +++++++ .../core/filesystem/ComboMount.java | 19 +------ .../core/filesystem/EmptyMount.java | 11 +--- .../core/filesystem/FileMount.java | 39 ++------------ .../core/filesystem/FileSystem.java | 8 +-- .../filesystem/FileSystemWrapperMount.java | 33 ++---------- .../core/filesystem/JarMount.java | 10 +--- .../core/filesystem/ResourceMount.java | 10 +--- .../core/filesystem/SubMount.java | 11 +--- .../modem/wired/WiredModemPeripheral.java | 2 +- .../turtle/upgrades/TurtleCraftingTable.java | 2 +- .../shared/turtle/upgrades/TurtleModem.java | 1 - .../shared/turtle/upgrades/TurtleSpeaker.java | 2 +- .../shared/turtle/upgrades/TurtleTool.java | 1 - .../core/ComputerTestDelegate.java | 2 +- .../core/filesystem/JarMountTest.java | 12 ++--- .../core/filesystem/MemoryMount.java | 25 +++++---- 26 files changed, 128 insertions(+), 292 deletions(-) create mode 100644 src/main/java/dan200/computercraft/api/peripheral/NotAttachedException.java rename src/main/java/dan200/computercraft/api/{ => turtle}/AbstractTurtleUpgrade.java (93%) diff --git a/src/main/java/dan200/computercraft/api/ComputerCraftAPI.java b/src/main/java/dan200/computercraft/api/ComputerCraftAPI.java index 5dfd95759..4eb417e8c 100644 --- a/src/main/java/dan200/computercraft/api/ComputerCraftAPI.java +++ b/src/main/java/dan200/computercraft/api/ComputerCraftAPI.java @@ -112,31 +112,6 @@ public final class ComputerCraftAPI return getInstance().createResourceMount( domain, subPath ); } - /** - * Creates a file system mount to a resource folder, and returns it. - * - * Use in conjunction with {@link IComputerAccess#mount} or {@link IComputerAccess#mountWritable} to mount a - * resource folder onto a computer's file system. - * - * The files in this mount will be a combination of files in all mod jar, and data packs that contain - * resources with the same domain and path. - * - * @param klass The mod class to which the files belong. - * @param domain The domain under which to look for resources. eg: "mymod". - * @param subPath The subPath under which to look for resources. eg: "lua/myfiles". - * @return The mount, or {@code null} if it could be created for some reason. - * @see IComputerAccess#mount(String, IMount) - * @see IComputerAccess#mountWritable(String, IWritableMount) - * @see IMount - * @deprecated Use {@link #createResourceMount(String, String)} instead. - */ - @Nullable - @Deprecated - public static IMount createResourceMount( Class klass, @Nonnull String domain, @Nonnull String subPath ) - { - return getInstance().createResourceMount( domain, subPath ); - } - /** * Registers a peripheral provider to convert blocks into {@link IPeripheral} implementations. * diff --git a/src/main/java/dan200/computercraft/api/filesystem/IMount.java b/src/main/java/dan200/computercraft/api/filesystem/IMount.java index 41fb51b78..50682a958 100644 --- a/src/main/java/dan200/computercraft/api/filesystem/IMount.java +++ b/src/main/java/dan200/computercraft/api/filesystem/IMount.java @@ -11,8 +11,6 @@ import net.minecraft.world.World; import javax.annotation.Nonnull; import java.io.IOException; -import java.io.InputStream; -import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.List; @@ -22,10 +20,10 @@ import java.util.List; * * Ready made implementations of this interface can be created using * {@link ComputerCraftAPI#createSaveDirMount(World, String, long)} or - * {@link ComputerCraftAPI#createResourceMount(Class, String, String)}, or you're free to implement it yourselves! + * {@link ComputerCraftAPI#createResourceMount(String, String)}, or you're free to implement it yourselves! * * @see ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see ComputerCraftAPI#createResourceMount(String, String) * @see IComputerAccess#mount(String, IMount) * @see IWritableMount */ @@ -67,18 +65,6 @@ public interface IMount */ long getSize( @Nonnull String path ) throws IOException; - /** - * Opens a file with a given path, and returns an {@link InputStream} representing its contents. - * - * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram". - * @return A stream representing the contents of the file. - * @throws IOException If the file does not exist, or could not be opened. - * @deprecated Use {@link #openChannelForRead(String)} instead - */ - @Nonnull - @Deprecated - InputStream openForRead( @Nonnull String path ) throws IOException; - /** * Opens a file with a given path, and returns an {@link ReadableByteChannel} representing its contents. * @@ -89,8 +75,5 @@ public interface IMount * @throws IOException If the file does not exist, or could not be opened. */ @Nonnull - default ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException - { - return Channels.newChannel( openForRead( path ) ); - } + ReadableByteChannel openForRead( @Nonnull String path ) throws IOException; } diff --git a/src/main/java/dan200/computercraft/api/filesystem/IWritableMount.java b/src/main/java/dan200/computercraft/api/filesystem/IWritableMount.java index a778f423d..8ec6fabc1 100644 --- a/src/main/java/dan200/computercraft/api/filesystem/IWritableMount.java +++ b/src/main/java/dan200/computercraft/api/filesystem/IWritableMount.java @@ -12,7 +12,6 @@ import net.minecraft.world.World; import javax.annotation.Nonnull; import java.io.IOException; import java.io.OutputStream; -import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; /** @@ -45,18 +44,6 @@ public interface IWritableMount extends IMount */ void delete( @Nonnull String path ) throws IOException; - /** - * Opens a file with a given path, and returns an {@link OutputStream} for writing to it. - * - * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram". - * @return A stream for writing to - * @throws IOException If the file could not be opened for writing. - * @deprecated Use {@link #openChannelForWrite(String)} instead. - */ - @Nonnull - @Deprecated - OutputStream openForWrite( @Nonnull String path ) throws IOException; - /** * Opens a file with a given path, and returns an {@link OutputStream} for writing to it. * @@ -66,22 +53,7 @@ public interface IWritableMount extends IMount * @throws IOException If the file could not be opened for writing. */ @Nonnull - default WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException - { - return Channels.newChannel( openForWrite( path ) ); - } - - /** - * Opens a file with a given path, and returns an {@link OutputStream} for appending to it. - * - * @param path A file path in normalised format, relative to the mount location. ie: "programs/myprogram". - * @return A stream for writing to. - * @throws IOException If the file could not be opened for writing. - * @deprecated Use {@link #openChannelForAppend(String)} instead. - */ - @Nonnull - @Deprecated - OutputStream openForAppend( @Nonnull String path ) throws IOException; + WritableByteChannel openForWrite( @Nonnull String path ) throws IOException; /** * Opens a file with a given path, and returns an {@link OutputStream} for appending to it. @@ -92,10 +64,7 @@ public interface IWritableMount extends IMount * @throws IOException If the file could not be opened for writing. */ @Nonnull - default WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException - { - return Channels.newChannel( openForAppend( path ) ); - } + WritableByteChannel openForAppend( @Nonnull String path ) throws IOException; /** * Get the amount of free space on the mount, in bytes. You should decrease this value as the user writes to the diff --git a/src/main/java/dan200/computercraft/api/media/IMedia.java b/src/main/java/dan200/computercraft/api/media/IMedia.java index b841a6500..3134f5bc6 100644 --- a/src/main/java/dan200/computercraft/api/media/IMedia.java +++ b/src/main/java/dan200/computercraft/api/media/IMedia.java @@ -79,7 +79,7 @@ public interface IMedia * @see IMount * @see dan200.computercraft.api.filesystem.IWritableMount * @see dan200.computercraft.api.ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see dan200.computercraft.api.ComputerCraftAPI#createResourceMount(Class, String, String) + * @see dan200.computercraft.api.ComputerCraftAPI#createResourceMount(String, String) */ @Nullable default IMount createDataMount( @Nonnull ItemStack stack, @Nonnull World world ) diff --git a/src/main/java/dan200/computercraft/api/peripheral/IComputerAccess.java b/src/main/java/dan200/computercraft/api/peripheral/IComputerAccess.java index 263f4d86b..9a6f243e9 100644 --- a/src/main/java/dan200/computercraft/api/peripheral/IComputerAccess.java +++ b/src/main/java/dan200/computercraft/api/peripheral/IComputerAccess.java @@ -14,7 +14,6 @@ import net.minecraft.world.World; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collections; import java.util.Map; /** @@ -31,9 +30,9 @@ public interface IComputerAccess * @param mount The mount object to mount on the computer. * @return The location on the computer's file system where you the mount mounted, or {@code null} if there was already a * file in the desired location. Store this value if you wish to unmount the mount later. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. * @see ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see ComputerCraftAPI#createResourceMount(String, String) * @see #mount(String, IMount, String) * @see #mountWritable(String, IWritableMount) * @see #unmount(String) @@ -53,9 +52,9 @@ public interface IComputerAccess * @param driveName A custom name to give for this mount location, as returned by {@code fs.getDrive()}. * @return The location on the computer's file system where you the mount mounted, or {@code null} if there was already a * file in the desired location. Store this value if you wish to unmount the mount later. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. * @see ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see ComputerCraftAPI#createResourceMount(String, String) * @see #mount(String, IMount) * @see #mountWritable(String, IWritableMount) * @see #unmount(String) @@ -71,9 +70,9 @@ public interface IComputerAccess * @param mount The mount object to mount on the computer. * @return The location on the computer's file system where you the mount mounted, or null if there was already a * file in the desired location. Store this value if you wish to unmount the mount later. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. * @see ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see ComputerCraftAPI#createResourceMount(String, String) * @see #mount(String, IMount) * @see #unmount(String) * @see IMount @@ -92,9 +91,9 @@ public interface IComputerAccess * @param driveName A custom name to give for this mount location, as returned by {@code fs.getDrive()}. * @return The location on the computer's file system where you the mount mounted, or null if there was already a * file in the desired location. Store this value if you wish to unmount the mount later. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. * @see ComputerCraftAPI#createSaveDirMount(World, String, long) - * @see ComputerCraftAPI#createResourceMount(Class, String, String) + * @see ComputerCraftAPI#createResourceMount(String, String) * @see #mount(String, IMount) * @see #unmount(String) * @see IMount @@ -114,8 +113,8 @@ public interface IComputerAccess * @param location The desired location in the computers file system of the directory to unmount. * This must be the location of a directory previously mounted by {@link #mount(String, IMount)} or * {@link #mountWritable(String, IWritableMount)}, as indicated by their return value. - * @throws RuntimeException If the peripheral has been detached. - * @throws RuntimeException If the mount does not exist, or was mounted by another peripheral. + * @throws NotAttachedException If the peripheral has been detached. + * @throws IllegalStateException If the mount does not exist, or was mounted by another peripheral. * @see #mount(String, IMount) * @see #mountWritable(String, IWritableMount) */ @@ -146,7 +145,7 @@ public interface IComputerAccess * to lua data types in the same fashion as the return values of IPeripheral.callMethod(). * * You may supply {@code null} to indicate that no arguments are to be supplied. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. * @see IPeripheral#callMethod */ void queueEvent( @Nonnull String event, @Nullable Object[] arguments ); @@ -159,7 +158,7 @@ public interface IComputerAccess * which peripheral the event came. * * @return A string unique to the computer, but not globally. - * @throws RuntimeException If the peripheral has been detached. + * @throws NotAttachedException If the peripheral has been detached. */ @Nonnull String getAttachmentName(); @@ -170,14 +169,12 @@ public interface IComputerAccess * This may include other peripherals on the wired network or peripherals on other sides of the computer. * * @return All reachable peripherals + * @throws NotAttachedException If the peripheral has been detached. * @see #getAttachmentName() * @see #getAvailablePeripheral(String) */ @Nonnull - default Map getAvailablePeripherals() - { - return Collections.emptyMap(); - } + Map getAvailablePeripherals(); /** * Get a reachable peripheral with the given attachment name. This is a equivalent to @@ -188,10 +185,7 @@ public interface IComputerAccess * @see #getAvailablePeripherals() */ @Nullable - default IPeripheral getAvailablePeripheral( @Nonnull String name ) - { - return null; - } + IPeripheral getAvailablePeripheral( @Nonnull String name ); /** * Get a {@link IWorkMonitor} for tasks your peripheral might execute on the main (server) thread. @@ -205,10 +199,8 @@ public interface IComputerAccess * thread. * * @return The work monitor for the main thread, or {@code null} if this computer does not have one. + * @throws NotAttachedException If the peripheral has been detached. */ - @Nullable - default IWorkMonitor getMainThreadMonitor() - { - return null; - } + @Nonnull + IWorkMonitor getMainThreadMonitor(); } diff --git a/src/main/java/dan200/computercraft/api/peripheral/NotAttachedException.java b/src/main/java/dan200/computercraft/api/peripheral/NotAttachedException.java new file mode 100644 index 000000000..08c343303 --- /dev/null +++ b/src/main/java/dan200/computercraft/api/peripheral/NotAttachedException.java @@ -0,0 +1,25 @@ +/* + * This file is part of the public ComputerCraft API - http://www.computercraft.info + * Copyright Daniel Ratcliffe, 2011-2020. This API may be redistributed unmodified and in full only. + * For help using the API, and posting your mods, visit the forums at computercraft.info. + */ +package dan200.computercraft.api.peripheral; + +/** + * Thrown when performing operations on {@link IComputerAccess} when the current peripheral is no longer attached to + * the computer. + */ +public class NotAttachedException extends IllegalStateException +{ + private static final long serialVersionUID = 1221244785535553536L; + + public NotAttachedException() + { + super( "You are not attached to this Computer" ); + } + + public NotAttachedException( String s ) + { + super( s ); + } +} diff --git a/src/main/java/dan200/computercraft/api/AbstractTurtleUpgrade.java b/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java similarity index 93% rename from src/main/java/dan200/computercraft/api/AbstractTurtleUpgrade.java rename to src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java index 94b2e701c..15bb43832 100644 --- a/src/main/java/dan200/computercraft/api/AbstractTurtleUpgrade.java +++ b/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java @@ -3,10 +3,8 @@ * Copyright Daniel Ratcliffe, 2011-2020. This API may be redistributed unmodified and in full only. * For help using the API, and posting your mods, visit the forums at computercraft.info. */ -package dan200.computercraft.api; +package dan200.computercraft.api.turtle; -import dan200.computercraft.api.turtle.ITurtleUpgrade; -import dan200.computercraft.api.turtle.TurtleUpgradeType; import net.minecraft.item.ItemStack; import net.minecraft.util.IItemProvider; import net.minecraft.util.ResourceLocation; diff --git a/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java b/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java index ad3c24da1..019333500 100644 --- a/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java +++ b/src/main/java/dan200/computercraft/core/apis/ComputerAccess.java @@ -13,7 +13,6 @@ import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystemException; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -123,7 +122,7 @@ public abstract class ComputerAccess implements IComputerAccess m_environment.queueEvent( event, arguments ); } - @Nullable + @Nonnull @Override public IWorkMonitor getMainThreadMonitor() { diff --git a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java index c289c85a2..81de3d594 100644 --- a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java @@ -11,6 +11,8 @@ import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.api.peripheral.IWorkMonitor; +import dan200.computercraft.api.peripheral.NotAttachedException; import dan200.computercraft.core.computer.ComputerSide; import dan200.computercraft.core.tracking.TrackingField; @@ -122,53 +124,35 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange @Override public synchronized String mount( @Nonnull String desiredLoc, @Nonnull IMount mount, @Nonnull String driveName ) { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } - + if( !m_attached ) throw new NotAttachedException(); return super.mount( desiredLoc, mount, driveName ); } @Override public synchronized String mountWritable( @Nonnull String desiredLoc, @Nonnull IWritableMount mount, @Nonnull String driveName ) { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } - + if( !m_attached ) throw new NotAttachedException(); return super.mountWritable( desiredLoc, mount, driveName ); } @Override public synchronized void unmount( String location ) { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } - + if( !m_attached ) throw new NotAttachedException(); super.unmount( location ); } @Override public int getID() { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } + if( !m_attached ) throw new NotAttachedException(); return super.getID(); } @Override public void queueEvent( @Nonnull final String event, final Object[] arguments ) { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } + if( !m_attached ) throw new NotAttachedException(); super.queueEvent( event, arguments ); } @@ -176,10 +160,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange @Override public String getAttachmentName() { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } + if( !m_attached ) throw new NotAttachedException(); return m_side; } @@ -187,10 +168,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange @Override public Map getAvailablePeripherals() { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } + if( !m_attached ) throw new NotAttachedException(); Map peripherals = new HashMap<>(); for( PeripheralWrapper wrapper : m_peripherals ) @@ -208,10 +186,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange @Override public IPeripheral getAvailablePeripheral( @Nonnull String name ) { - if( !m_attached ) - { - throw new RuntimeException( "You are not attached to this Computer" ); - } + if( !m_attached ) throw new NotAttachedException(); for( PeripheralWrapper wrapper : m_peripherals ) { @@ -222,6 +197,14 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange } return null; } + + @Nonnull + @Override + public IWorkMonitor getMainThreadMonitor() + { + if( !m_attached ) throw new NotAttachedException(); + return super.getMainThreadMonitor(); + } } private final IAPIEnvironment m_environment; diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java b/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java index c5cb4f529..ee4917afe 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerSystem.java @@ -9,12 +9,15 @@ import dan200.computercraft.api.filesystem.IFileSystem; import dan200.computercraft.api.lua.IComputerSystem; import dan200.computercraft.api.lua.ILuaAPIFactory; import dan200.computercraft.api.peripheral.IComputerAccess; +import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.core.apis.ComputerAccess; import dan200.computercraft.core.apis.IAPIEnvironment; import dan200.computercraft.core.filesystem.FileSystem; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Map; /** * Implementation of {@link IComputerAccess}/{@link IComputerSystem} for usage by externally registered APIs. @@ -54,4 +57,19 @@ public class ComputerSystem extends ComputerAccess implements IComputerSystem { return environment.getLabel(); } + + @Nonnull + @Override + public Map getAvailablePeripherals() + { + // TODO: Should this return peripherals on the current computer? + return Collections.emptyMap(); + } + + @Nullable + @Override + public IPeripheral getAvailablePeripheral( @Nonnull String name ) + { + return null; + } } diff --git a/src/main/java/dan200/computercraft/core/filesystem/ComboMount.java b/src/main/java/dan200/computercraft/core/filesystem/ComboMount.java index 53e119954..94630a90e 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/ComboMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/ComboMount.java @@ -10,7 +10,6 @@ import dan200.computercraft.api.filesystem.IMount; import javax.annotation.Nonnull; import java.io.IOException; -import java.io.InputStream; import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.HashSet; @@ -115,8 +114,7 @@ public class ComboMount implements IMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { for( int i = m_parts.length - 1; i >= 0; --i ) { @@ -128,19 +126,4 @@ public class ComboMount implements IMount } throw new FileOperationException( path, "No such file" ); } - - @Nonnull - @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException - { - for( int i = m_parts.length - 1; i >= 0; --i ) - { - IMount part = m_parts[i]; - if( part.exists( path ) && !part.isDirectory( path ) ) - { - return part.openChannelForRead( path ); - } - } - throw new FileOperationException( path, "No such file" ); - } } diff --git a/src/main/java/dan200/computercraft/core/filesystem/EmptyMount.java b/src/main/java/dan200/computercraft/core/filesystem/EmptyMount.java index 15d6df7f1..3fd2dd3b9 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/EmptyMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/EmptyMount.java @@ -10,7 +10,6 @@ import dan200.computercraft.api.filesystem.IMount; import javax.annotation.Nonnull; import java.io.IOException; -import java.io.InputStream; import java.nio.channels.ReadableByteChannel; import java.util.List; @@ -42,15 +41,7 @@ public class EmptyMount implements IMount @Nonnull @Override @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException - { - throw new FileOperationException( path, "No such file" ); - } - - @Nonnull - @Override - @Deprecated - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { throw new FileOperationException( path, "No such file" ); } diff --git a/src/main/java/dan200/computercraft/core/filesystem/FileMount.java b/src/main/java/dan200/computercraft/core/filesystem/FileMount.java index eb0f39ff8..414fdef75 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/FileMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/FileMount.java @@ -10,7 +10,8 @@ import dan200.computercraft.api.filesystem.FileOperationException; import dan200.computercraft.api.filesystem.IWritableMount; import javax.annotation.Nonnull; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.file.Files; @@ -199,21 +200,7 @@ public class FileMount implements IWritableMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException - { - if( created() ) - { - File file = getRealPath( path ); - if( file.exists() && !file.isDirectory() ) return new FileInputStream( file ); - } - - throw new FileOperationException( path, "No such file" ); - } - - @Nonnull - @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { if( created() ) { @@ -299,23 +286,7 @@ public class FileMount implements IWritableMount @Nonnull @Override - @Deprecated - public OutputStream openForWrite( @Nonnull String path ) throws IOException - { - return Channels.newOutputStream( openChannelForWrite( path ) ); - } - - @Nonnull - @Override - @Deprecated - public OutputStream openForAppend( @Nonnull String path ) throws IOException - { - return Channels.newOutputStream( openChannelForAppend( path ) ); - } - - @Nonnull - @Override - public WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException + public WritableByteChannel openForWrite( @Nonnull String path ) throws IOException { create(); File file = getRealPath( path ); @@ -336,7 +307,7 @@ public class FileMount implements IWritableMount @Nonnull @Override - public WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException + public WritableByteChannel openForAppend( @Nonnull String path ) throws IOException { if( !created() ) { diff --git a/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java b/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java index 4f6f84cd3..b9819b587 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java +++ b/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java @@ -165,7 +165,7 @@ public class FileSystem { if( m_mount.exists( path ) && !m_mount.isDirectory( path ) ) { - return m_mount.openChannelForRead( path ); + return m_mount.openForRead( path ); } else { @@ -245,7 +245,7 @@ public class FileSystem m_writableMount.makeDirectory( dir ); } } - return m_writableMount.openChannelForWrite( path ); + return m_writableMount.openForWrite( path ); } } catch( AccessDeniedException e ) @@ -275,7 +275,7 @@ public class FileSystem m_writableMount.makeDirectory( dir ); } } - return m_writableMount.openChannelForWrite( path ); + return m_writableMount.openForWrite( path ); } else if( m_mount.isDirectory( path ) ) { @@ -283,7 +283,7 @@ public class FileSystem } else { - return m_writableMount.openChannelForAppend( path ); + return m_writableMount.openForAppend( path ); } } catch( AccessDeniedException e ) diff --git a/src/main/java/dan200/computercraft/core/filesystem/FileSystemWrapperMount.java b/src/main/java/dan200/computercraft/core/filesystem/FileSystemWrapperMount.java index 3b270fe1b..40e4634d8 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/FileSystemWrapperMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/FileSystemWrapperMount.java @@ -9,9 +9,6 @@ import dan200.computercraft.api.filesystem.IFileSystem; import javax.annotation.Nonnull; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.util.Collections; @@ -55,7 +52,7 @@ public class FileSystemWrapperMount implements IFileSystem @Nonnull @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { try { @@ -70,7 +67,7 @@ public class FileSystemWrapperMount implements IFileSystem @Nonnull @Override - public WritableByteChannel openChannelForWrite( @Nonnull String path ) throws IOException + public WritableByteChannel openForWrite( @Nonnull String path ) throws IOException { try { @@ -84,7 +81,7 @@ public class FileSystemWrapperMount implements IFileSystem @Nonnull @Override - public WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException + public WritableByteChannel openForAppend( @Nonnull String path ) throws IOException { try { @@ -96,30 +93,6 @@ public class FileSystemWrapperMount implements IFileSystem } } - @Nonnull - @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException - { - return Channels.newInputStream( openChannelForRead( path ) ); - } - - @Nonnull - @Override - @Deprecated - public OutputStream openForWrite( @Nonnull String path ) throws IOException - { - return Channels.newOutputStream( openChannelForWrite( path ) ); - } - - @Nonnull - @Override - @Deprecated - public OutputStream openForAppend( @Nonnull String path ) throws IOException - { - return Channels.newOutputStream( openChannelForAppend( path ) ); - } - @Override public long getRemainingSpace() throws IOException { diff --git a/src/main/java/dan200/computercraft/core/filesystem/JarMount.java b/src/main/java/dan200/computercraft/core/filesystem/JarMount.java index 336d4ddcc..874a3d536 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/JarMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/JarMount.java @@ -181,15 +181,7 @@ public class JarMount implements IMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException - { - return Channels.newInputStream( openChannelForRead( path ) ); - } - - @Nonnull - @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { FileEntry file = get( path ); if( file != null && !file.isDirectory() ) diff --git a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java b/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java index e6116c144..fbc3a5e7d 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/ResourceMount.java @@ -223,15 +223,7 @@ public final class ResourceMount implements IMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException - { - return Channels.newInputStream( openChannelForRead( path ) ); - } - - @Nonnull - @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { FileEntry file = get( path ); if( file != null && !file.isDirectory() ) diff --git a/src/main/java/dan200/computercraft/core/filesystem/SubMount.java b/src/main/java/dan200/computercraft/core/filesystem/SubMount.java index 1cdac4a6a..e59b22878 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/SubMount.java +++ b/src/main/java/dan200/computercraft/core/filesystem/SubMount.java @@ -9,7 +9,6 @@ import dan200.computercraft.api.filesystem.IMount; import javax.annotation.Nonnull; import java.io.IOException; -import java.io.InputStream; import java.nio.channels.ReadableByteChannel; import java.util.List; @@ -52,19 +51,11 @@ public class SubMount implements IMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) throws IOException + public ReadableByteChannel openForRead( @Nonnull String path ) throws IOException { return m_parent.openForRead( getFullPath( path ) ); } - @Nonnull - @Override - public ReadableByteChannel openChannelForRead( @Nonnull String path ) throws IOException - { - return m_parent.openChannelForRead( getFullPath( path ) ); - } - private String getFullPath( String path ) { if( path.isEmpty() ) diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java index 5030b9c21..88223b71d 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemPeripheral.java @@ -374,7 +374,7 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW m_computer.queueEvent( event, arguments ); } - @Nullable + @Nonnull @Override public IWorkMonitor getMainThreadMonitor() { diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleCraftingTable.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleCraftingTable.java index 6267dff39..e5ad307b6 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleCraftingTable.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleCraftingTable.java @@ -5,9 +5,9 @@ */ package dan200.computercraft.shared.turtle.upgrades; -import dan200.computercraft.api.AbstractTurtleUpgrade; import dan200.computercraft.api.client.TransformedModel; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.api.turtle.AbstractTurtleUpgrade; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleUpgradeType; diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java index a8bf2ca0a..ab74c7994 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleModem.java @@ -6,7 +6,6 @@ package dan200.computercraft.shared.turtle.upgrades; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.AbstractTurtleUpgrade; import dan200.computercraft.api.client.TransformedModel; import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.*; diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java index 581af58b5..a06016bc0 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleSpeaker.java @@ -7,9 +7,9 @@ package dan200.computercraft.shared.turtle.upgrades; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.AbstractTurtleUpgrade; import dan200.computercraft.api.client.TransformedModel; import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.api.turtle.AbstractTurtleUpgrade; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.api.turtle.TurtleUpgradeType; diff --git a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java index 57738a357..81d3f8e00 100644 --- a/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java +++ b/src/main/java/dan200/computercraft/shared/turtle/upgrades/TurtleTool.java @@ -6,7 +6,6 @@ package dan200.computercraft.shared.turtle.upgrades; import dan200.computercraft.ComputerCraft; -import dan200.computercraft.api.AbstractTurtleUpgrade; import dan200.computercraft.api.client.TransformedModel; import dan200.computercraft.api.turtle.*; import dan200.computercraft.api.turtle.event.TurtleAttackEvent; diff --git a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java index 2ffc3bcd2..15b477b6a 100644 --- a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java +++ b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java @@ -86,7 +86,7 @@ public class ComputerTestDelegate for( String child : children ) mount.delete( child ); // And add our startup file - try( WritableByteChannel channel = mount.openChannelForWrite( "startup.lua" ); + try( WritableByteChannel channel = mount.openForWrite( "startup.lua" ); Writer writer = Channels.newWriter( channel, StandardCharsets.UTF_8.newEncoder(), -1 ) ) { writer.write( "loadfile('test/mcfly.lua', nil, _ENV)('test/spec') cct_test.finish()" ); diff --git a/src/test/java/dan200/computercraft/core/filesystem/JarMountTest.java b/src/test/java/dan200/computercraft/core/filesystem/JarMountTest.java index 31031c96c..cf1c61529 100644 --- a/src/test/java/dan200/computercraft/core/filesystem/JarMountTest.java +++ b/src/test/java/dan200/computercraft/core/filesystem/JarMountTest.java @@ -13,14 +13,14 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import static org.junit.jupiter.api.Assertions.*; -@SuppressWarnings( "deprecation" ) public class JarMountTest { private static final File ZIP_FILE = new File( "test-files/jar-mount.zip" ); @@ -63,9 +63,9 @@ public class JarMountTest { IMount mount = new JarMount( ZIP_FILE, "dir/file.lua" ); byte[] contents; - try( InputStream stream = mount.openForRead( "" ) ) + try( ReadableByteChannel stream = mount.openForRead( "" ) ) { - contents = ByteStreams.toByteArray( stream ); + contents = ByteStreams.toByteArray( Channels.newInputStream( stream ) ); } assertEquals( new String( contents, StandardCharsets.UTF_8 ), "print('testing')" ); @@ -76,9 +76,9 @@ public class JarMountTest { IMount mount = new JarMount( ZIP_FILE, "dir" ); byte[] contents; - try( InputStream stream = mount.openForRead( "file.lua" ) ) + try( ReadableByteChannel stream = mount.openForRead( "file.lua" ) ) { - contents = ByteStreams.toByteArray( stream ); + contents = ByteStreams.toByteArray( Channels.newInputStream( stream ) ); } assertEquals( new String( contents, StandardCharsets.UTF_8 ), "print('testing')" ); diff --git a/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java b/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java index dcdc40b5e..bd16104ac 100644 --- a/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java +++ b/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java @@ -6,9 +6,15 @@ package dan200.computercraft.core.filesystem; import dan200.computercraft.api.filesystem.IWritableMount; +import dan200.computercraft.core.apis.handles.ArrayByteChannel; import javax.annotation.Nonnull; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; import java.util.*; /** @@ -61,10 +67,9 @@ public class MemoryMount implements IWritableMount @Nonnull @Override - @Deprecated - public OutputStream openForWrite( @Nonnull final String path ) + public WritableByteChannel openForWrite( @Nonnull final String path ) { - return new ByteArrayOutputStream() + return Channels.newChannel( new ByteArrayOutputStream() { @Override public void close() throws IOException @@ -72,13 +77,12 @@ public class MemoryMount implements IWritableMount super.close(); files.put( path, toByteArray() ); } - }; + } ); } @Nonnull @Override - @Deprecated - public OutputStream openForAppend( @Nonnull final String path ) throws IOException + public WritableByteChannel openForAppend( @Nonnull final String path ) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream() { @@ -93,7 +97,7 @@ public class MemoryMount implements IWritableMount byte[] current = files.get( path ); if( current != null ) stream.write( current ); - return stream; + return Channels.newChannel( stream ); } @Override @@ -131,10 +135,9 @@ public class MemoryMount implements IWritableMount @Nonnull @Override - @Deprecated - public InputStream openForRead( @Nonnull String path ) + public ReadableByteChannel openForRead( @Nonnull String path ) { - return new ByteArrayInputStream( files.get( path ) ); + return new ArrayByteChannel( files.get( path ) ); } public MemoryMount addFile( String file, String contents )