Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoerg Kubitz2021-06-18 10:09:43 +0000
committerAndrey Loskutov2021-06-18 22:08:59 +0000
commit2b7c3edd9892af020cb0925039813d7383aacb63 (patch)
treeaa794c09e9167a554ced2d169013829706997de4
parent298e47435e19b1217e5593f3c29652720857f6e1 (diff)
downloadeclipse.jdt.core-2b7c3edd9892af020cb0925039813d7383aacb63.tar.gz
eclipse.jdt.core-2b7c3edd9892af020cb0925039813d7383aacb63.tar.xz
eclipse.jdt.core-2b7c3edd9892af020cb0925039813d7383aacb63.zip
Bug 573239 - use JDK 11 API where appropriateI20210619-1800I20210618-1920I20210618-1800
That code was always only meant to provide BREE 8 compatibility. Change-Id: Ifeec0b951e9f3f00041d902370a2c5034de22ef6 Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de> Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/182165 Tested-by: JDT Bot <jdt-bot@eclipse.org> Reviewed-by: Andrey Loskutov <loskutov@gmx.de>
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/CharArray.java20
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java71
2 files changed, 3 insertions, 88 deletions
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/CharArray.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/CharArray.java
index f6ac05ea33..38b7fef8d1 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/CharArray.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/CharArray.java
@@ -31,31 +31,13 @@ final class CharArray implements Comparable<CharArray> {
@Override
public int compareTo(CharArray o) {
- return compare(this.key, o.key);
+ return Arrays.compare(this.key, o.key);
}
public char[] getKey() {
return this.key;
}
- // Can be replaced with Arrays.compare once JDT core moves to BREE >= 9
- private static int compare(char[] a, char[] b) {
- if (a == b) {
- return 0;
- }
- if (a == null || b == null) {
- return a == null ? -1 : 1;
- }
- int shortestLenght = Math.min(a.length, b.length);
- for (int i = 0; i < shortestLenght; i++) {
- if (a[i] != b[i]) {
- return Character.compare(a[i], b[i]);
- }
- }
-
- return a.length - b.length;
- }
-
@Override
public boolean equals(Object obj) {
if (this == obj) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
index f7ab82b7d5..4c7fb19cfe 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
@@ -234,7 +234,6 @@ public class Util implements SuffixConstants {
String displayString(Object o);
}
- private static final int DEFAULT_READING_SIZE = 8192;
private static final int DEFAULT_WRITING_SIZE = 1024;
public final static String UTF_8 = "UTF-8"; //$NON-NLS-1$
public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
@@ -470,61 +469,9 @@ public class Util implements SuffixConstants {
* @throws IOException if a problem occurred reading the stream.
*/
public static byte[] getInputStreamAsByteArray(InputStream input) throws IOException {
- if (input instanceof ByteArrayInputStream) {
- // not available in java 8: ((ByteArrayInputStream) input).readAllBytes();
- int length = ((ByteArrayInputStream) input).available();
- return readNBytes(input, length);
- }
- if (input instanceof FileInputStream) {
- long length = ((FileInputStream) input).getChannel().size();
- return readNBytes(input, length);
- }
- return readAllBytes(input);
- }
-
- private static byte[] readAllBytes(InputStream input) throws IOException {
- ArrayList<byte[]> byteBufList = new ArrayList<byte[]>(3);
- int totalByteCount = 0;
- int bytesJustRead;
- do {
- int bufLength = Math.max(input.available(), DEFAULT_READING_SIZE); // read at least 8K
- byte[] byteBuf = new byte[bufLength];
- int bytesInBuf = 0;
- int byteTransferSize = bufLength;
- while ((bytesJustRead = input.read(byteBuf, bytesInBuf, byteTransferSize)) >= 0) {
- bytesInBuf += bytesJustRead;
- totalByteCount += bytesJustRead;
- byteTransferSize = bufLength - bytesInBuf;
- if (byteTransferSize <= 0)
- break;
- }
- if (bytesInBuf>0)
- byteBufList.add(byteBuf);
- } while (bytesJustRead >= 0);
- // final concatenation of buffers:
- if (byteBufList.size()==1) {
- byte[] firstBuf = byteBufList.get(0);
- if (firstBuf.length >= totalByteCount) { // fast path
- return (firstBuf.length == totalByteCount) ? firstBuf : Arrays.copyOf(firstBuf, totalByteCount);
- }
- }
- byte[] result = new byte[totalByteCount];
- int byteCount = 0;
- for (byte[] byteBuf : byteBufList) {
- int byteTransferSize = Math.min(totalByteCount - byteCount, byteBuf.length);
- System.arraycopy(byteBuf, 0, result, byteCount, byteTransferSize);
- byteCount += byteTransferSize;
- }
- return result;
+ return input.readAllBytes(); // will have even slighly better performance as of JDK17+ see JDK-8264777
}
- public static final int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8;
-
- public static byte[] readNBytes(java.io.InputStream input, long length) throws IOException {
- if (length > MAX_ARRAY_LENGTH) // fail fast
- throw new OutOfMemoryError("File too large for array: " + length); //$NON-NLS-1$
- return readNBytes(input, (int) length);
- }
/**
* Returns the given input stream's first bytes as array.
@@ -532,21 +479,7 @@ public class Util implements SuffixConstants {
* @throws IOException if a problem occurred reading the stream.
*/
public static byte[] readNBytes(InputStream input, int byteLength) throws IOException {
- // InputStream.readNBytes() only available after java 11
- if (byteLength == 0)
- return new byte[0];
- byte[] byteBuf = new byte[byteLength]; // exact buffer size
- int byteCount = 0;
- int byteTransferSize = byteBuf.length;
- int bytesRead;
- while ((bytesRead = input.read(byteBuf, byteCount, byteTransferSize)) >= 0) {
- byteCount += bytesRead;
- byteTransferSize = byteBuf.length - byteCount;
- if (byteTransferSize <= 0) {
- break;
- }
- }
- return (byteBuf.length == byteCount) ? byteBuf : Arrays.copyOf(byteBuf, byteCount);
+ return input.readNBytes(byteLength);
}
private static Map<String, byte[]> bomByEncoding = new HashMap<String, byte[]>();

Back to the top