Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/java.base/share/classes/java/util/zip/ZipUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,6 +39,7 @@
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.ByteArrayLittleEndian;

class ZipUtils {

Expand Down Expand Up @@ -170,23 +171,23 @@ static LocalDateTime javaEpochToLocalDateTime(long time) {
* The bytes are assumed to be in Intel (little-endian) byte order.
*/
public static final int get16(byte[] b, int off) {
return (b[off] & 0xff) | ((b[off + 1] & 0xff) << 8);
return ByteArrayLittleEndian.getUnsignedShort(b, off);
}

/**
* Fetches unsigned 32-bit value from byte array at specified offset.
* The bytes are assumed to be in Intel (little-endian) byte order.
*/
public static final long get32(byte[] b, int off) {
return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
return ByteArrayLittleEndian.getUnsignedInt(b, off);
}

/**
* Fetches signed 64-bit value from byte array at specified offset.
* The bytes are assumed to be in Intel (little-endian) byte order.
*/
public static final long get64(byte[] b, int off) {
return get32(b, off) | (get32(b, off+4) << 32);
return ByteArrayLittleEndian.getLong(b, off);
}

/**
Expand All @@ -195,7 +196,7 @@ public static final long get64(byte[] b, int off) {
*
*/
public static final int get32S(byte[] b, int off) {
return (get16(b, off) | (get16(b, off+2) << 16));
return ByteArrayLittleEndian.getInt(b, off);
}

// fields access methods
Expand All @@ -204,15 +205,15 @@ static final int CH(byte[] b, int n) {
}

static final int SH(byte[] b, int n) {
return (b[n] & 0xff) | ((b[n + 1] & 0xff) << 8);
return ByteArrayLittleEndian.getUnsignedShort(b, n);
}

static final long LG(byte[] b, int n) {
return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL;
return ByteArrayLittleEndian.getUnsignedInt(b, n);
}

static final long LL(byte[] b, int n) {
return (LG(b, n)) | (LG(b, n + 4) << 32);
return ByteArrayLittleEndian.getLong(b, n);
}

static final long GETSIG(byte[] b) {
Expand Down
36 changes: 35 additions & 1 deletion src/java.base/share/classes/jdk/internal/util/ByteArray.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -132,6 +132,23 @@ public static int getInt(byte[] array, int offset) {
return (int) INT.get(array, offset);
}

/**
* {@return an {@code unsigned int} from the provided {@code array} at the given {@code offset}
* using big endian order}.
* <p>
* There are no access alignment requirements.
*
* @param array to get a value from.
* @param offset where extraction in the array should begin
* @return an {@code long} representing an unsigned int from the array
* @throws IndexOutOfBoundsException if the provided {@code offset} is outside
* the range [0, array.length - 2]
* @see #setUnsignedShort(byte[], int, int)
*/
public static long getUnsignedInt(byte[] array, int offset) {
return Integer.toUnsignedLong((int) INT.get(array, offset));
}

/**
* {@return a {@code float} from the provided {@code array} at the given {@code offset}
* using big endian order}.
Expand Down Expand Up @@ -316,6 +333,23 @@ public static void setInt(byte[] array, int offset, int value) {
INT.set(array, offset, value);
}

/**
* Sets (writes) the provided {@code value} using big endian order into
* the provided {@code array} beginning at the given {@code offset}.
* <p>
* There are no access alignment requirements.
*
* @param array to set (write) a value into
* @param offset where setting (writing) in the array should begin
* @param value value to set in the array
* @throws IndexOutOfBoundsException if the provided {@code offset} is outside
* the range [0, array.length - 4]
* @see #getUnsignedInt(byte[], int)
*/
public static void setUnsignedInt(byte[] array, int offset, long value) {
INT.set(array, offset, (int) value);
}

/**
* Sets (writes) the provided {@code value} using big endian order into
* the provided {@code array} beginning at the given {@code offset}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -132,6 +132,23 @@ public static int getInt(byte[] array, int offset) {
return (int) INT.get(array, offset);
}

/**
* {@return an {@code unsigned int} from the provided {@code array} at the given {@code offset}
* using little endian order}.
* <p>
* There are no access alignment requirements.
*
* @param array to get a value from.
* @param offset where extraction in the array should begin
* @return an {@code int} representing an unsigned short from the array
* @throws IndexOutOfBoundsException if the provided {@code offset} is outside
* the range [0, array.length - 4]
* @see #setUnsignedInt(byte[], int, long)
*/
public static long getUnsignedInt(byte[] array, int offset) {
return Integer.toUnsignedLong((int) INT.get(array, offset));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Glavo, I was going to recommend adding a test method to existing jtreg tests to test these new methods. But it looks like there's no jtreg test for this ByteArrayLittleEndian class. Would you mind creating a new test class to (at least) test these methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Glavo, I was going to recommend adding a test method to existing jtreg tests to test these new methods. But it looks like there's no jtreg test for this ByteArrayLittleEndian class. Would you mind creating a new test class to (at least) test these methods?

I think I could modify test/jdk/jdk/internal/util/ByteArray/ReadWriteValues.java to also test ByteArrayLittleEndian.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for ByteArrayLittleEndian in #14636


/**
* {@return a {@code float} from the provided {@code array} at the given {@code offset}
* using little endian order}.
Expand Down Expand Up @@ -316,6 +333,23 @@ public static void setInt(byte[] array, int offset, int value) {
INT.set(array, offset, value);
}

/**
* Sets (writes) the provided {@code value} using little endian order into
* the provided {@code array} beginning at the given {@code offset}.
* <p>
* There are no access alignment requirements.
*
* @param array to set (write) a value into
* @param offset where setting (writing) in the array should begin
* @param value value to set in the array
* @throws IndexOutOfBoundsException if the provided {@code offset} is outside
* the range [0, array.length - 4]
* @see #getUnsignedInt(byte[], int)
*/
public static void setUnsignedInt(byte[] array, int offset, long value) {
INT.set(array, offset, (int) value);
}

/**
* Sets (writes) the provided {@code value} using little endian order into
* the provided {@code array} beginning at the given {@code offset}.
Expand Down
Loading