From 3f7e65f70a4674e49696623263e32670f3ecdbfb Mon Sep 17 00:00:00 2001 From: "yineng.huang" Date: Fri, 28 Feb 2025 13:49:51 +0800 Subject: [PATCH] =?UTF-8?q?InputStreamUtils=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../upperComputer/utils/InputStreamUtils.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/utils/InputStreamUtils.java diff --git a/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/utils/InputStreamUtils.java b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/utils/InputStreamUtils.java new file mode 100644 index 0000000..e09ca39 --- /dev/null +++ b/cctp-atu/atu-upper-computer/src/main/java/net/northking/cctp/upperComputer/utils/InputStreamUtils.java @@ -0,0 +1,128 @@ +package net.northking.cctp.upperComputer.utils; + + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.function.BiFunction; + +/** + * @author : yineng.huang + * @date : 2024/10/12 16:08 + */ +public class InputStreamUtils { + + private static final int DEFAULT_BUFFER_SIZE = 8192; + + private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; + + + public static int readNBytes(InputStream inputStream,byte[] b, int off, int len) throws IOException { + checkFromIndexSize(off, len, b.length); + int n = 0; + while (n < len) { + int count = read(inputStream,b, off + n, len - n); + if (count < 0) + break; + n += count; + } + return n; + } + + public static int read(InputStream inputStream,byte b[], int off, int len) throws IOException { + checkFromIndexSize(off, len, b.length); + if (len == 0) { + return 0; + } + + int c = inputStream.read(); + if (c == -1) { + return -1; + } + b[off] = (byte)c; + + int i = 1; + try { + for (; i < len ; i++) { + c = inputStream.read(); + if (c == -1) { + break; + } + b[off + i] = (byte)c; + } + } catch (IOException ee) { + } + return i; + } + + public static int checkFromIndexSize(int fromIndex, int size, int length) { + return Preconditions.checkFromIndexSize(fromIndex, size, length, null); + } + + public static byte[] readNBytes(InputStream inputStream,int len) throws IOException { + if (len < 0) { + throw new IllegalArgumentException("len < 0"); + } + + List bufs = null; + byte[] result = null; + int total = 0; + int remaining = len; + int n; + do { + byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)]; + int nread = 0; + + // read to EOF which may read more or less than buffer size + while ((n = inputStream.read(buf, nread, + Math.min(buf.length - nread, remaining))) > 0) { + nread += n; + remaining -= n; + } + + if (nread > 0) { + if (MAX_BUFFER_SIZE - total < nread) { + throw new OutOfMemoryError("Required array size too large"); + } + if (nread < buf.length) { + buf = Arrays.copyOfRange(buf, 0, nread); + } + total += nread; + if (result == null) { + result = buf; + } else { + if (bufs == null) { + bufs = new ArrayList<>(); + bufs.add(result); + } + bufs.add(buf); + } + } + // if the last call to read returned -1 or the number of bytes + // requested have been read then break + } while (n >= 0 && remaining > 0); + + if (bufs == null) { + if (result == null) { + return new byte[0]; + } + return result.length == total ? + result : Arrays.copyOf(result, total); + } + + result = new byte[total]; + int offset = 0; + remaining = total; + for (byte[] b : bufs) { + int count = Math.min(b.length, remaining); + System.arraycopy(b, 0, result, offset, count); + offset += count; + remaining -= count; + } + + return result; + } +}