Skip to content

Commit

Permalink
jamesmudd#113 Fix Reading of UTF8 fixed length datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmudd authored and JCzogalla committed Jan 24, 2020
1 parent 1d29e6f commit eb41492
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java
Expand Up @@ -28,6 +28,7 @@
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

import static java.nio.charset.StandardCharsets.US_ASCII;
Expand Down Expand Up @@ -130,8 +131,9 @@ public static Object readDataset(DataType type, ByteBuffer buffer, int[] dimensi
"Unsupported floating point type size " + floatingPoint.getSize() + " bytes");
}
} else if (type instanceof StringData) {
int stringLength = type.getSize();
fillFixedLengthStringData(data, dimensions, buffer, stringLength);
final int stringLength = type.getSize();
final Charset charset = ((StringData) type).getCharset();
fillFixedLengthStringData(data, dimensions, buffer, stringLength, charset);
} else if (type instanceof Reference) {
//reference type handles addresses, which are always longs for this library
int size = type.getSize();
Expand Down Expand Up @@ -323,17 +325,17 @@ private static void fillData(Object data, int[] dims, DoubleBuffer buffer) {

// String Data

private static void fillFixedLengthStringData(Object data, int[] dims, ByteBuffer buffer, int stringLength) {
private static void fillFixedLengthStringData(Object data, int[] dims, ByteBuffer buffer, int stringLength, Charset charset) {
if (dims.length > 1) {
for (int i = 0; i < dims[0]; i++) {
Object newArray = Array.get(data, i);
fillFixedLengthStringData(newArray, stripLeadingIndex(dims), buffer, stringLength);
fillFixedLengthStringData(newArray, stripLeadingIndex(dims), buffer, stringLength, charset);
}
} else {
for (int i = 0; i < dims[0]; i++) {
buffer.position(i * stringLength);
ByteBuffer elementBuffer = Utils.createSubBuffer(buffer, stringLength);
Array.set(data, i, US_ASCII.decode(elementBuffer).toString().trim());
Array.set(data, i, charset.decode(elementBuffer).toString().trim());
}
}
}
Expand Down

0 comments on commit eb41492

Please sign in to comment.