io.ipfs.api.NamedStreamable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-xpx-ipfs-api Show documentation
Show all versions of java-xpx-ipfs-api Show documentation
The ProximaX IPFS Java API is a Java library for interacting IPFS server.
package io.ipfs.api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public interface NamedStreamable
{
InputStream getInputStream() throws IOException;
Optional getName();
List getChildren();
boolean isDirectory();
default byte[] getContents() throws IOException {
InputStream in = getInputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmp = new byte[4096];
int r;
while ((r=in.read(tmp))>= 0)
bout.write(tmp, 0, r);
return bout.toByteArray();
}
class FileWrapper implements NamedStreamable {
private final File source;
public FileWrapper(File source) {
this.source = source;
}
public InputStream getInputStream() throws IOException {
return new FileInputStream(source);
}
public boolean isDirectory() {
return source.isDirectory();
}
@Override
public List getChildren() {
return isDirectory() ?
Stream.of(source.listFiles())
.map(NamedStreamable.FileWrapper::new)
.collect(Collectors.toList()) :
Collections.emptyList();
}
public Optional getName() {
try {
return Optional.of(URLEncoder.encode(source.getName(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
class ByteArrayWrapper implements NamedStreamable {
private final Optional name;
private final byte[] data;
public ByteArrayWrapper(byte[] data) {
this(Optional.empty(), data);
}
public ByteArrayWrapper(String name, byte[] data) {
this(Optional.of(name), data);
}
public ByteArrayWrapper(Optional name, byte[] data) {
this.name = name;
this.data = data;
}
public boolean isDirectory() {
return false;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
@Override
public List getChildren() {
return Collections.emptyList();
}
public Optional getName() {
return name;
}
}
class DirWrapper implements NamedStreamable {
private final String name;
private final List children;
public DirWrapper(String name, List children) {
this.name = name;
this.children = children;
}
@Override
public InputStream getInputStream() throws IOException {
throw new IllegalStateException("Cannot get an input stream for a directory!");
}
@Override
public Optional getName() {
return Optional.of(name);
}
@Override
public List getChildren() {
return children;
}
@Override
public boolean isDirectory() {
return true;
}
}
class InputStreamWrapper implements NamedStreamable {
private final Optional name;
private final InputStream inputStream;
public InputStreamWrapper(InputStream inputStream) {
this(Optional.empty(), inputStream);
}
public InputStreamWrapper(String name, InputStream inputStream) {
this(Optional.of(name), inputStream);
}
public InputStreamWrapper(Optional name, InputStream inputStream) {
this.name = name;
this.inputStream = inputStream;
}
@Override
public boolean isDirectory() {
return false;
}
@Override
public InputStream getInputStream() {
return inputStream;
}
@Override
public List getChildren() {
return Collections.emptyList();
}
@Override
public Optional getName() {
return this.name;
}
}
}