Skip to content

Commit

Permalink
Prevent java.nio class conflict bn java 8 and 11
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 1, 2024
1 parent 18cecaa commit 248bb8a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/reandroid/archive/ArchiveFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZipFileInput>{

Expand All @@ -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());
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/com/reandroid/archive/io/ArchiveFileEntrySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZipFileInput> {

Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/reandroid/archive/io/ZipFileInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/reandroid/archive/io/ZipFileOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/reandroid/utils/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 248bb8a

Please sign in to comment.