Skip to content

Add NIO ClipboardFormat methods, and deprecate File ones #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion verification/src/changes/accepted-core-public-api-changes.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,14 @@
"FIELD_REMOVED_IN_SUPERCLASS"
]
}
]
],
"Acceptable addition of default method in interface": [
{
"type": "com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat",
"member": "Method com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat.isFormat(java.nio.file.Path)",
"changes": [
"METHOD_NEW_DEFAULT"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static void main(String[] args) {
if (file.getName().endsWith("level.dat")) {
throw new IllegalArgumentException("level.dat file support is unfinished.");
} else {
ClipboardFormat format = ClipboardFormats.findByFile(file);
ClipboardFormat format = ClipboardFormats.findByPath(file.toPath());
if (format != null) {
int dataVersion;
if (format != BuiltInClipboardFormat.MCEDIT_SCHEMATIC) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void load(Actor actor, LocalSession session,
return;
}

ClipboardFormat inferredFormat = ClipboardFormats.findByFile(f);
ClipboardFormat inferredFormat = ClipboardFormats.findByPath(f.toPath());
if (inferredFormat != null) {
format = inferredFormat;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

/**
Expand Down Expand Up @@ -68,9 +69,21 @@ public interface ClipboardFormat {
*
* @param file the file
* @return true if the given file is of this format
* @deprecated Use {@link #isFormat(Path)} instead
*/
@Deprecated
default boolean isFormat(File file) {
try (InputStream stream = Files.newInputStream(file.toPath())) {
return isFormat(file.toPath());
}

/**
* Return whether the given path is of this format.
*
* @param path the path
* @return true if the given path is of this format
*/
default boolean isFormat(Path path) {
try (InputStream stream = Files.newInputStream(path)) {
return isFormat(stream);
} catch (IOException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -72,8 +73,7 @@ public static void registerClipboardFormat(ClipboardFormat format) {
/**
* Find the clipboard format named by the given alias.
*
* @param alias
* the alias
* @param alias the alias
* @return the format, otherwise null if none is matched
*/
@Nullable
Expand All @@ -85,16 +85,30 @@ public static ClipboardFormat findByAlias(String alias) {
/**
* Detect the format of a given file.
*
* @param file
* the file
* @param file the file
* @return the format, otherwise null if one cannot be detected
* @deprecated Use {@link #findByPath(Path)} instead.
*/
@Deprecated
@Nullable
public static ClipboardFormat findByFile(File file) {
checkNotNull(file);

return findByPath(file.toPath());
}

/**
* Detect the format of a given path.
*
* @param path the path
* @return the format, otherwise null if one cannot be detected
*/
@Nullable
public static ClipboardFormat findByPath(Path path) {
checkNotNull(path);

for (ClipboardFormat format : registeredFormats) {
if (format.isFormat(file)) {
if (format.isFormat(path)) {
return format;
}
}
Expand Down
Loading