app: Do code cleanup
This commit is contained in:
1 parent
9dcc24f4d9
commit
191db14398
72 files changed
+471
-506
No files matched your search
@@ -18,7 +18,6 @@ package com.hippo.unifile;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.system.ErrnoException;
|
||||
import android.system.Os;
|
||||
@@ -34,196 +33,195 @@ import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
class FileDescriptorRandomAccessFile implements UniRandomAccessFile {
|
||||
|
||||
@IntDef({FLUSH_NONE, FLUSH_FSYNC, FLUSH_FDATASYNC})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface FlushMode {}
|
||||
public static final int FLUSH_NONE = 0;
|
||||
public static final int FLUSH_FSYNC = 1;
|
||||
public static final int FLUSH_FDATASYNC = 2;
|
||||
private final ParcelFileDescriptor pfd;
|
||||
private final FileDescriptor fd;
|
||||
private final int flushAfterWrite;
|
||||
private final Object closeLock = new Object();
|
||||
private volatile boolean closed = false;
|
||||
private FileDescriptorRandomAccessFile(ParcelFileDescriptor pfd, FileDescriptor fd, String mode) {
|
||||
this.pfd = pfd;
|
||||
this.fd = fd;
|
||||
|
||||
public static final int FLUSH_NONE = 0;
|
||||
public static final int FLUSH_FSYNC = 1;
|
||||
public static final int FLUSH_FDATASYNC = 2;
|
||||
|
||||
private final ParcelFileDescriptor pfd;
|
||||
private final FileDescriptor fd;
|
||||
private final int flushAfterWrite;
|
||||
private final Object closeLock = new Object();
|
||||
private volatile boolean closed = false;
|
||||
|
||||
private FileDescriptorRandomAccessFile(ParcelFileDescriptor pfd, FileDescriptor fd, String mode) {
|
||||
this.pfd = pfd;
|
||||
this.fd = fd;
|
||||
|
||||
switch (mode) {
|
||||
case "rws":
|
||||
flushAfterWrite = FLUSH_FSYNC;
|
||||
break;
|
||||
case "rwd":
|
||||
flushAfterWrite = FLUSH_FDATASYNC;
|
||||
break;
|
||||
default:
|
||||
flushAfterWrite = FLUSH_NONE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void maybeSync() {
|
||||
if (flushAfterWrite == FLUSH_FSYNC) {
|
||||
try {
|
||||
fd.sync();
|
||||
} catch (IOException e) {
|
||||
// Ignored
|
||||
}
|
||||
} else if (flushAfterWrite == FLUSH_FDATASYNC) {
|
||||
try {
|
||||
Os.fdatasync(fd);
|
||||
} catch (ErrnoException e) {
|
||||
// Ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
synchronized (closeLock) {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
switch (mode) {
|
||||
case "rws":
|
||||
flushAfterWrite = FLUSH_FSYNC;
|
||||
break;
|
||||
case "rwd":
|
||||
flushAfterWrite = FLUSH_FDATASYNC;
|
||||
break;
|
||||
default:
|
||||
flushAfterWrite = FLUSH_NONE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pfd.close();
|
||||
@NonNull
|
||||
static FileDescriptorRandomAccessFile create(Context context, Uri uri, String mode) throws IOException {
|
||||
// Get FileDescriptor
|
||||
ParcelFileDescriptor pfd;
|
||||
try {
|
||||
pfd = context.getContentResolver().openFileDescriptor(uri, mode);
|
||||
} catch (Throwable e) {
|
||||
Utils.throwIfFatal(e);
|
||||
throw new IOException("Can't get ParcelFileDescriptor", e);
|
||||
}
|
||||
if (pfd == null) {
|
||||
throw new IOException("Can't get ParcelFileDescriptor");
|
||||
}
|
||||
FileDescriptor fd;
|
||||
try {
|
||||
fd = pfd.getFileDescriptor();
|
||||
} catch (SecurityException e) {
|
||||
pfd.close();
|
||||
throw new IOException("Can't get FileDescriptor", e);
|
||||
}
|
||||
if (fd == null) {
|
||||
pfd.close();
|
||||
throw new IOException("Can't get FileDescriptor");
|
||||
}
|
||||
|
||||
try {
|
||||
Os.close(fd);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilePointer() throws IOException {
|
||||
try {
|
||||
return Os.lseek(fd, 0L, OsConstants.SEEK_CUR);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void seek(long pos) throws IOException {
|
||||
if (pos < 0) {
|
||||
throw new IOException("offset < 0: " + pos);
|
||||
} else {
|
||||
try {
|
||||
Os.lseek(fd, pos, OsConstants.SEEK_SET);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int skipBytes(int n) throws IOException {
|
||||
long pos;
|
||||
long len;
|
||||
long newpos;
|
||||
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
pos = getFilePointer();
|
||||
len = length();
|
||||
newpos = pos + n;
|
||||
if (newpos > len) {
|
||||
newpos = len;
|
||||
}
|
||||
seek(newpos);
|
||||
|
||||
/* return the actual number of bytes skipped */
|
||||
return (int) (newpos - pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long length() throws IOException {
|
||||
try {
|
||||
return Os.fstat(fd).st_size;
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLength(long newLength) throws IOException {
|
||||
if (newLength < 0) {
|
||||
throw new IllegalArgumentException("newLength < 0");
|
||||
}
|
||||
try {
|
||||
Os.ftruncate(fd, newLength);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
return new FileDescriptorRandomAccessFile(pfd, fd, mode);
|
||||
}
|
||||
|
||||
long filePointer = getFilePointer();
|
||||
if (filePointer > newLength) {
|
||||
seek(newLength);
|
||||
private void maybeSync() {
|
||||
if (flushAfterWrite == FLUSH_FSYNC) {
|
||||
try {
|
||||
fd.sync();
|
||||
} catch (IOException e) {
|
||||
// Ignored
|
||||
}
|
||||
} else if (flushAfterWrite == FLUSH_FDATASYNC) {
|
||||
try {
|
||||
Os.fdatasync(fd);
|
||||
} catch (ErrnoException e) {
|
||||
// Ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
maybeSync();
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
synchronized (closeLock) {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
pfd.close();
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
try {
|
||||
return Os.read(fd, b, off, len);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
try {
|
||||
Os.write(fd, b, off, len);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
maybeSync();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
static FileDescriptorRandomAccessFile create(Context context, Uri uri, String mode) throws IOException {
|
||||
// Get FileDescriptor
|
||||
ParcelFileDescriptor pfd;
|
||||
try {
|
||||
pfd = context.getContentResolver().openFileDescriptor(uri, mode);
|
||||
} catch (Throwable e) {
|
||||
Utils.throwIfFatal(e);
|
||||
throw new IOException("Can't get ParcelFileDescriptor", e);
|
||||
}
|
||||
if (pfd == null) {
|
||||
throw new IOException("Can't get ParcelFileDescriptor");
|
||||
}
|
||||
FileDescriptor fd;
|
||||
try {
|
||||
fd = pfd.getFileDescriptor();
|
||||
} catch (SecurityException e) {
|
||||
pfd.close();
|
||||
throw new IOException("Can't get FileDescriptor", e);
|
||||
}
|
||||
if (fd == null) {
|
||||
pfd.close();
|
||||
throw new IOException("Can't get FileDescriptor");
|
||||
try {
|
||||
Os.close(fd);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
return new FileDescriptorRandomAccessFile(pfd, fd, mode);
|
||||
}
|
||||
@Override
|
||||
public long getFilePointer() throws IOException {
|
||||
try {
|
||||
return Os.lseek(fd, 0L, OsConstants.SEEK_CUR);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void seek(long pos) throws IOException {
|
||||
if (pos < 0) {
|
||||
throw new IOException("offset < 0: " + pos);
|
||||
} else {
|
||||
try {
|
||||
Os.lseek(fd, pos, OsConstants.SEEK_SET);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int skipBytes(int n) throws IOException {
|
||||
long pos;
|
||||
long len;
|
||||
long newpos;
|
||||
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
pos = getFilePointer();
|
||||
len = length();
|
||||
newpos = pos + n;
|
||||
if (newpos > len) {
|
||||
newpos = len;
|
||||
}
|
||||
seek(newpos);
|
||||
|
||||
/* return the actual number of bytes skipped */
|
||||
return (int) (newpos - pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long length() throws IOException {
|
||||
try {
|
||||
return Os.fstat(fd).st_size;
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLength(long newLength) throws IOException {
|
||||
if (newLength < 0) {
|
||||
throw new IllegalArgumentException("newLength < 0");
|
||||
}
|
||||
try {
|
||||
Os.ftruncate(fd, newLength);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
|
||||
long filePointer = getFilePointer();
|
||||
if (filePointer > newLength) {
|
||||
seek(newLength);
|
||||
}
|
||||
|
||||
maybeSync();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
try {
|
||||
return Os.read(fd, b, off, len);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
write(b, 0, b.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) throws IOException {
|
||||
try {
|
||||
Os.write(fd, b, off, len);
|
||||
} catch (ErrnoException e) {
|
||||
throw new IOException("An ErrnoException occurs", e);
|
||||
}
|
||||
maybeSync();
|
||||
}
|
||||
|
||||
@IntDef({FLUSH_NONE, FLUSH_FSYNC, FLUSH_FDATASYNC})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface FlushMode {
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user