Skip to content

Commit

Permalink
[vmutils] Add unit tests for Locale.decodeString().
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 20, 2017
1 parent 6358efd commit 78aec38
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.MissingResourceException;
Expand Down Expand Up @@ -65,10 +66,49 @@ public final class Locale {

private static final int BUFFER_SIZE = 2048;

private static Charset[] decodingCharsets;

static {
final Charset def = Charset.defaultCharset();
final List<Charset> list = new ArrayList<>();
Charset c;
c = Charset.forName("IBM437"); //$NON-NLS-1$
if (c != null && !def.equals(c)) {
list.add(c);
}
c = Charset.forName("ISO-8859-1"); //$NON-NLS-1$
if (c != null && !def.equals(c)) {
list.add(c);
}
c = Charset.forName("UTF-8"); //$NON-NLS-1$
if (c != null && !def.equals(c)) {
list.add(c);
}
list.add(def);
decodingCharsets = new Charset[list.size()];
list.toArray(decodingCharsets);
}

private Locale() {
//
}

/** Replies the charsets for decoding that must be used prior to the installed charsets.
*
* @return the priorized decoding charsets.
*/
public static Charset[] getPriorizedDecodingCharsets() {
return decodingCharsets;
}

/** Change the charsets for decoding that must be used prior to the installed charsets.
*
* @param charsets the priorized decoding charsets.
*/
public static void setPriorizedDecodingCharsets(Charset[] charsets) {
decodingCharsets = charsets;
}

private static Class<?> detectResourceClass(Class<?> resource) {
if (resource == null) {
// Parameter value:
Expand Down Expand Up @@ -386,20 +426,17 @@ public static String getStringWithDefault(ClassLoader classLoader, String key, S
*/
@Pure
public static String decodeString(byte[] bytes) {
final Charset defaultCharset = Charset.defaultCharset();
final Charset westEuropean = Charset.forName("ISO-8859-1"); //$NON-NLS-1$
final Charset utf = Charset.forName("UTF-8"); //$NON-NLS-1$
final Charset[] prior = getPriorizedDecodingCharsets();

final String refBuffer = new String(bytes);

CharBuffer buffer = decodeString(bytes, defaultCharset, refBuffer.length());

if ((buffer == null) && (!defaultCharset.equals(westEuropean))) {
buffer = decodeString(bytes, westEuropean, refBuffer.length());
}
CharBuffer buffer = null;

if ((buffer == null) && (!defaultCharset.equals(utf))) {
buffer = decodeString(bytes, utf, refBuffer.length());
for (final Charset charset : prior) {
buffer = decodeString(bytes, charset, refBuffer.length());
if (buffer != null) {
break;
}
}

if (buffer == null) {
Expand All @@ -413,9 +450,9 @@ public static String decodeString(byte[] bytes) {
}
// Use the default encoding
if (buffer == null) {
return refBuffer;
return refBuffer.trim();
}
return buffer.toString();
return buffer.toString().trim();
}

/** Decode the specified array of bytes with the specified charset.
Expand Down Expand Up @@ -443,7 +480,7 @@ private static CharBuffer decodeString(byte[] bytes, Charset charset, int refere
return null;
}
}
// Apply a proprietaty detection
// Apply a proprietary detection
buffer.position(0);
char c;
int type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,11 @@ public void getStringWithRawFormatStyleWithFormat7() {
Locale.getString("FORMAT_7", data)); //$NON-NLS-1$
}

@Test
public void decodeStringByteArray() {
byte[] bytes = new byte[] {80, 104, 111, 116, 111, 103, 114, 97, 109, 109, -126, 116, 114,
105, 101, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32};
String str = Locale.decodeString(bytes);
assertEquals("Photogrammétrie", str); //$NON-NLS-1$
}
}

0 comments on commit 78aec38

Please sign in to comment.