diff --git a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java index e744e00e3a6..5c037eadbf2 100644 --- a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java +++ b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java @@ -44,10 +44,9 @@ private String readString() { if (stringNbBytes == 0) { return null; } - byte[] stringBytes = new byte[stringNbBytes]; - int nbBytesRead = dis.read(stringBytes, 0, stringNbBytes); - if (nbBytesRead != stringNbBytes) { - throw new PowsyblException("Cannot read the full string, bytes missing: " + (stringNbBytes - nbBytesRead)); + byte[] stringBytes = dis.readNBytes(stringNbBytes); + if (stringBytes.length != stringNbBytes) { + throw new PowsyblException("Cannot read the full string, bytes missing: " + (stringNbBytes - stringBytes.length)); } return new String(stringBytes, StandardCharsets.UTF_8); } catch (IOException e) {