From 248bb8aa677ddb60b021d3b2ea667ef6a1d12c01 Mon Sep 17 00:00:00 2001 From: REAndroid Date: Fri, 1 Nov 2024 12:29:30 +0100 Subject: [PATCH] Prevent java.nio class conflict bn java 8 and 11 --- .../java/com/reandroid/archive/ArchiveFile.java | 5 +---- .../archive/io/ArchiveFileEntrySource.java | 13 ++----------- .../com/reandroid/archive/io/ZipFileInput.java | 7 ++----- .../com/reandroid/archive/io/ZipFileOutput.java | 5 +++-- .../common/FileChannelInputStream.java | 9 +++++---- .../java/com/reandroid/utils/io/FileUtil.java | 17 +++++++++++++++++ 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/reandroid/archive/ArchiveFile.java b/src/main/java/com/reandroid/archive/ArchiveFile.java index 6fe9cc744..84a275f12 100644 --- a/src/main/java/com/reandroid/archive/ArchiveFile.java +++ b/src/main/java/com/reandroid/archive/ArchiveFile.java @@ -22,7 +22,6 @@ import java.io.File; import java.io.IOException; import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; public class ArchiveFile extends Archive{ @@ -39,9 +38,7 @@ InputSource createInputSource(ArchiveEntry entry) { } @Override void extractStored(File file, ArchiveEntry archiveEntry) throws IOException { - FileUtil.createNewFile(file); - StandardOpenOption openOption = StandardOpenOption.WRITE; - FileChannel outputChannel = FileChannel.open(file.toPath(), openOption); + FileChannel outputChannel = FileUtil.openWriteChannel(file); FileChannel fileChannel = getZipInput().getFileChannel(); fileChannel.position(archiveEntry.getFileOffset()); outputChannel.transferFrom(fileChannel, 0, archiveEntry.getDataSize()); diff --git a/src/main/java/com/reandroid/archive/io/ArchiveFileEntrySource.java b/src/main/java/com/reandroid/archive/io/ArchiveFileEntrySource.java index 0acbda2dc..c48933975 100644 --- a/src/main/java/com/reandroid/archive/io/ArchiveFileEntrySource.java +++ b/src/main/java/com/reandroid/archive/io/ArchiveFileEntrySource.java @@ -17,12 +17,12 @@ import com.reandroid.archive.Archive; import com.reandroid.archive.ArchiveEntry; +import com.reandroid.utils.io.FileUtil; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; public class ArchiveFileEntrySource extends ArchiveEntrySource { @@ -57,16 +57,7 @@ public void write(File file) throws IOException { super.write(file); return; } - File dir = file.getParentFile(); - if(dir != null && !dir.exists()){ - dir.mkdirs(); - } - if(file.isFile()){ - file.delete(); - } - file.createNewFile(); - StandardOpenOption openOption = StandardOpenOption.WRITE; - FileChannel outputChannel = FileChannel.open(file.toPath(), openOption); + FileChannel outputChannel = FileUtil.openWriteChannel(file); outputChannel.transferFrom(fileChannel, 0, getLength()); outputChannel.close(); } diff --git a/src/main/java/com/reandroid/archive/io/ZipFileInput.java b/src/main/java/com/reandroid/archive/io/ZipFileInput.java index aeed3b099..4eeb474ab 100644 --- a/src/main/java/com/reandroid/archive/io/ZipFileInput.java +++ b/src/main/java/com/reandroid/archive/io/ZipFileInput.java @@ -16,11 +16,11 @@ package com.reandroid.archive.io; import com.reandroid.common.FileChannelInputStream; +import com.reandroid.utils.io.FileUtil; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; public class ZipFileInput extends ZipInput { private final File file; @@ -78,10 +78,7 @@ public FileChannel getFileChannel() throws IOException { return fileChannel; } synchronized (this){ - if(!file.isFile()){ - throw new FileNotFoundException("No such file: " + file); - } - fileChannel = FileChannel.open(this.file.toPath(), StandardOpenOption.READ); + fileChannel = FileUtil.openReadChannel(file); this.fileChannel = fileChannel; return fileChannel; } diff --git a/src/main/java/com/reandroid/archive/io/ZipFileOutput.java b/src/main/java/com/reandroid/archive/io/ZipFileOutput.java index 964c0e07b..3db63168c 100644 --- a/src/main/java/com/reandroid/archive/io/ZipFileOutput.java +++ b/src/main/java/com/reandroid/archive/io/ZipFileOutput.java @@ -15,12 +15,13 @@ */ package com.reandroid.archive.io; +import com.reandroid.utils.io.FileUtil; + import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; public class ZipFileOutput extends ZipOutput{ private final File file; @@ -69,7 +70,7 @@ private FileChannel getFileChannel() throws IOException { return fileChannel; } synchronized (this){ - fileChannel = FileChannel.open(this.file.toPath(), StandardOpenOption.WRITE); + fileChannel = FileUtil.openWriteChannel(file); this.fileChannel = fileChannel; return fileChannel; } diff --git a/src/main/java/com/reandroid/common/FileChannelInputStream.java b/src/main/java/com/reandroid/common/FileChannelInputStream.java index bd455581a..45fb838f6 100644 --- a/src/main/java/com/reandroid/common/FileChannelInputStream.java +++ b/src/main/java/com/reandroid/common/FileChannelInputStream.java @@ -15,13 +15,14 @@ */ package com.reandroid.common; +import com.reandroid.utils.io.FileUtil; + import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; public class FileChannelInputStream extends InputStream { private final FileChannel fileChannel; @@ -61,15 +62,15 @@ public FileChannelInputStream(FileChannel fileChannel, long length) throws IOExc this(fileChannel, length, DEFAULT_BUFFER_SIZE); } public FileChannelInputStream(File file, long length, int bufferSize) throws IOException { - this(FileChannel.open(file.toPath(), StandardOpenOption.READ), length, bufferSize); + this(FileUtil.openReadChannel(file), length, bufferSize); this.mAutoClosable = true; } public FileChannelInputStream(File file, byte[] buffer, int bufferSize) throws IOException { - this(FileChannel.open(file.toPath(), StandardOpenOption.READ), buffer, bufferSize); + this(FileUtil.openReadChannel(file), buffer, bufferSize); this.mAutoClosable = true; } public FileChannelInputStream(File file) throws IOException { - this(FileChannel.open(file.toPath(), StandardOpenOption.READ), file.length()); + this(FileUtil.openReadChannel(file), file.length()); this.mAutoClosable = true; } diff --git a/src/main/java/com/reandroid/utils/io/FileUtil.java b/src/main/java/com/reandroid/utils/io/FileUtil.java index 0579bece0..19b4bab79 100644 --- a/src/main/java/com/reandroid/utils/io/FileUtil.java +++ b/src/main/java/com/reandroid/utils/io/FileUtil.java @@ -20,6 +20,7 @@ import com.reandroid.utils.StringsUtil; import java.io.*; +import java.nio.channels.FileChannel; import java.util.HashMap; import java.util.Map; @@ -164,6 +165,22 @@ public static String toReadableFileSize(long size){ } return result + "." + dec + unit; } + public static FileChannel openReadChannel(File file) throws IOException { + if(!file.isFile()){ + throw new FileNotFoundException("No such file: " + file); + } + return new FileInputStream(file).getChannel(); + } + public static FileChannel openWriteChannel(File file) throws IOException { + if (file.isDirectory()) { + throw new IOException("File is directory: " + file); + } + ensureParentDirectory(file); + if (!file.exists() || file.delete()) { + file.createNewFile(); + } + return new FileOutputStream(file).getChannel(); + } public static InputStream inputStream(File file) throws IOException{ if(!file.isFile()){ throw new FileNotFoundException("No such file: " + file);