Support SAF properly

This commit is contained in:
wb9688 2020-06-13 17:29:57 +02:00 committed by Stypox
parent 1e09a1768e
commit 0f75024e03
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
23 changed files with 451 additions and 311 deletions

View file

@ -0,0 +1,48 @@
package org.schabi.newpipe.streams.io;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.InputStream;
public class SharpInputStream extends InputStream {
private final SharpStream stream;
public SharpInputStream(final SharpStream stream) throws IOException {
if (!stream.canRead()) {
throw new IOException("SharpStream is not readable");
}
this.stream = stream;
}
@Override
public int read() throws IOException {
return stream.read();
}
@Override
public int read(@NonNull final byte[] b) throws IOException {
return stream.read(b);
}
@Override
public int read(@NonNull final byte[] b, final int off, final int len) throws IOException {
return stream.read(b, off, len);
}
@Override
public long skip(final long n) throws IOException {
return stream.skip(n);
}
@Override
public int available() {
final long res = stream.available();
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) res;
}
@Override
public void close() {
stream.close();
}
}

View file

@ -0,0 +1,42 @@
package org.schabi.newpipe.streams.io;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.OutputStream;
public class SharpOutputStream extends OutputStream {
private final SharpStream stream;
public SharpOutputStream(final SharpStream stream) throws IOException {
if (!stream.canWrite()) {
throw new IOException("SharpStream is not writable");
}
this.stream = stream;
}
@Override
public void write(final int b) throws IOException {
stream.write((byte) b);
}
@Override
public void write(@NonNull final byte[] b) throws IOException {
stream.write(b);
}
@Override
public void write(@NonNull final byte[] b, final int off, final int len) throws IOException {
stream.write(b, off, len);
}
@Override
public void flush() throws IOException {
stream.flush();
}
@Override
public void close() {
stream.close();
}
}

View file

@ -1,12 +1,13 @@
package org.schabi.newpipe.streams.io;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
/**
* Based on C#'s Stream class.
*/
public abstract class SharpStream implements Closeable {
public abstract class SharpStream implements Closeable, Flushable {
public abstract int read() throws IOException;
public abstract int read(byte[] buffer) throws IOException;