Skip to content
Merged
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
32 changes: 29 additions & 3 deletions functional/MBCS_Tests/charsets/src/CharsetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CharsetsTest {
static Class<?> ArrayEncoder = null;
static Method ArrayDecoder_decode = null;
static Method ArrayEncoder_encode = null;
static Method ArrayEncoder_encodeFromUTF16 = null;

static int err_cnt = 0;

Expand All @@ -41,9 +42,20 @@ public class CharsetsTest {
ArrayEncoder = Class.forName("sun.nio.cs.ArrayEncoder");
ArrayDecoder_decode = ArrayDecoder.getDeclaredMethod("decode",
byte[].class, int.class, int.class, char[].class);
ArrayEncoder_encode = ArrayEncoder.getDeclaredMethod("encode",
char[].class, int.class, int.class, byte[].class);
ArrayDecoder_decode.setAccessible(true);

// JDK 26+ uses encodeFromUTF16, earlier versions use encode
if (JavaVersion.getFeature() >= 26) {
ArrayEncoder_encodeFromUTF16 = ArrayEncoder.getDeclaredMethod("encodeFromUTF16",
byte[].class, int.class, int.class, byte[].class, int.class);
ArrayEncoder_encodeFromUTF16.setAccessible(true);
} else {
ArrayEncoder_encode = ArrayEncoder.getDeclaredMethod("encode",
char[].class, int.class, int.class, byte[].class);
ArrayEncoder_encode.setAccessible(true);
}
} catch (Exception e) {
e.printStackTrace();
}
{
Vector<TreeSet[]> vector = new Vector<TreeSet[]>();
Expand Down Expand Up @@ -223,8 +235,22 @@ static void encode_data(CharsetEncoder ce) throws Exception {
}
char[] ca = Character.toChars(i);
ce.reset();

byte[] ba = new byte[(int)Math.ceil(ce.maxBytesPerChar()*ca.length)];
int len = (int)ArrayEncoder_encode.invoke(ArrayEncoder.cast(ce),ca,0,ca.length,ba);
int len;

// JDK 26+ uses encodeFromUTF16, earlier versions use encode
if (JavaVersion.getFeature() >= 26) {
// Convert char[] to UTF-16 LE byte array for the new API
byte[] utf16Bytes = new byte[ca.length * 2];
for (int j = 0; j < ca.length; j++) {
utf16Bytes[j * 2] = (byte)(ca[j] & 0xFF);
utf16Bytes[j * 2 + 1] = (byte)(ca[j] >> 8);
}
len = (int)ArrayEncoder_encodeFromUTF16.invoke(ArrayEncoder.cast(ce), utf16Bytes, 0, ca.length, ba, 0);
} else {
len = (int)ArrayEncoder_encode.invoke(ArrayEncoder.cast(ce), ca, 0, ca.length, ba);
}
StringBuffer sb0 = new StringBuffer();
for(int j=0; j<len; j++) sb0.append(String.format("\\x%02X", (int)ba[j] & 0xFF));
StringBuffer sb1 = new StringBuffer();
Expand Down
66 changes: 66 additions & 0 deletions functional/MBCS_Tests/charsets/src/JavaVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

import java.lang.reflect.Method;

public class JavaVersion {
final static long version;
final static int feature;
final static int interim;
final static int update;

static {
int tempFeature = 0;
int tempInterim = 0;
int tempUpdate = 0;
try {
Class<?> runtimeClass = Class.forName("java.lang.Runtime");
Method versionMid = runtimeClass.getDeclaredMethod("version", (Class<?>[])null);
Object ver = versionMid.invoke(null, (Object[])null);
Method featureMid = ver.getClass().getDeclaredMethod("feature", (Class<?>[])null);
Method interimMid = ver.getClass().getDeclaredMethod("interim", (Class<?>[])null);
Method updateMid = ver.getClass().getDeclaredMethod("update", (Class<?>[])null);
tempFeature = (int)featureMid.invoke(ver, (Object[])null);
tempInterim = (int)interimMid.invoke(ver, (Object[])null);
tempUpdate = (int)updateMid.invoke(ver, (Object[])null);
} catch (Exception e) {
}
feature = tempFeature;
interim = tempInterim;
update = tempUpdate;
version = feature * 1000000L +
interim * 1000L +
update;
}

public static long getVersion() {
return version;
}

public static long getFeature() {
return feature;
}

public static long getInterim() {
return interim;
}

public static long getUpdate() {
return update;
}

public static void main(String[] args) {
System.out.println(getVersion());
}
}