org.rx.io.IOStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.io;
import io.netty.buffer.ByteBuf;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.rx.annotation.ErrorCode;
import org.rx.core.*;
import org.rx.exception.ApplicationException;
import java.io.*;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import static org.rx.core.Extends.*;
@Slf4j
public abstract class IOStream extends Disposable implements Closeable, Flushable, Extends {
private static final long serialVersionUID = 3204673656139586437L;
public static final int NON_READ_FULLY = -1;
public static IOStream wrap(String filePath) {
return wrap(new File(filePath));
}
public static IOStream wrap(@NonNull File file) {
return new FileStream(file);
}
public static IOStream wrap(String name, byte[] data) {
HybridStream stream = new HybridStream();
stream.setName(ifNull(name, Strings.EMPTY));
stream.write(data);
return stream.rewind();
}
public static IOStream wrap(String name, InputStream in) {
HybridStream stream = new HybridStream();
stream.setName(ifNull(name, Strings.EMPTY));
stream.write(in);
return stream.rewind();
}
@SneakyThrows
public static long copy(@NonNull InputStream in, long length, @NonNull OutputStream out) {
byte[] buffer = Bytes.arrayBuffer();
boolean readFully = length != NON_READ_FULLY;
long copyLen = 0;
int read;
while ((!readFully || copyLen < length) && (read = in.read(buffer, 0, buffer.length)) != Constants.IO_EOF) {
out.write(buffer, 0, read);
copyLen += read;
}
out.flush();
return copyLen;
}
public static long checksum(byte[] bytes) {
CRC32 crc32 = new CRC32();
crc32.update(bytes, 0, bytes.length);
return crc32.getValue();
}
public static long checksum(InputStream stream) {
return checksum(stream, 1024);
}
@SneakyThrows
public static long checksum(InputStream stream, int bufferSize) {
CheckedInputStream checkedInputStream = new CheckedInputStream(stream, new CRC32());
byte[] buffer = new byte[bufferSize];
while (checkedInputStream.read(buffer, 0, buffer.length) >= 0) {
}
return checkedInputStream.getChecksum().getValue();
}
@SneakyThrows
public static String readString(@NonNull InputStream in, Charset charset) {
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
ByteBuf buf = Bytes.heapBuffer();
try {
byte[] sb = Bytes.arrayBuffer();
int r;
while ((r = in.read(sb)) != Constants.IO_EOF) {
buf.writeBytes(sb, 0, r);
}
return buf.toString(charset);
} finally {
buf.release();
}
//截断会出现乱码
// StringBuilder result = new StringBuilder();
// byte[] buffer = Bytes.arrayBuffer();
// int read;
// while ((read = in.read(buffer)) > 0) {
// String s = new String(buffer, 0, read, charset);
// result.append(s);
// }
// return result.toString();
}
@SneakyThrows
public static void writeString(@NonNull OutputStream out, @NonNull String value, Charset charset) {
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
out.write(value.getBytes(charset));
}
static int safeRemaining(long remaining) {
return remaining >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) remaining;
}
//jdk11 --add-opens java.base/java.lang=ALL-UNNAMED
public static void release(ByteBuffer buffer) {
if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) {
return;
}
invoke(invoke(viewed(buffer), "cleaner"), "clean");
}
private static Object invoke(final Object target, final String methodName, final Class>... args) {
return AccessController.doPrivileged((PrivilegedAction