From c2dc52ad3483fe96d01b8177253badc00c1c9017 Mon Sep 17 00:00:00 2001 From: Al Niessner Date: Thu, 26 Jan 2023 10:13:31 -0800 Subject: [PATCH] changes needed to support validate 519 Need to be able to extract the length of a record requiring interface and then implementer updates. Found an error in the computation of bytes. Added more detail to the error messaging. --- .../gov/nasa/pds/label/object/TableRecord.java | 5 +++++ .../pds/objectAccess/ByteWiseFileAccessor.java | 17 ++++++++++++++--- .../pds/objectAccess/DelimitedTableRecord.java | 6 ++++++ .../nasa/pds/objectAccess/FixedTableRecord.java | 3 +++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/gov/nasa/pds/label/object/TableRecord.java b/src/main/java/gov/nasa/pds/label/object/TableRecord.java index 77d5c282..d10b83b2 100644 --- a/src/main/java/gov/nasa/pds/label/object/TableRecord.java +++ b/src/main/java/gov/nasa/pds/label/object/TableRecord.java @@ -370,6 +370,11 @@ public interface TableRecord { */ void clear(); + /** + * @returns Length of the record in bytes. + */ + int length(); + /** * * @return Gets the record location. diff --git a/src/main/java/gov/nasa/pds/objectAccess/ByteWiseFileAccessor.java b/src/main/java/gov/nasa/pds/objectAccess/ByteWiseFileAccessor.java index 44b6d44c..c5178284 100644 --- a/src/main/java/gov/nasa/pds/objectAccess/ByteWiseFileAccessor.java +++ b/src/main/java/gov/nasa/pds/objectAccess/ByteWiseFileAccessor.java @@ -190,10 +190,21 @@ private void initializeAccessor(URL url, long offset, int length, long records, // if for whatever reason we don't read in sufficient bytes if (this.totalBytesRead < expectedBytesToRead) { if (expectedBytesToRead > fileSizeMinusOffset) { + if ((fileSizeMinusOffset / length) * length == fileSizeMinusOffset) + throw new InvalidTableException( + "Data object is truncated. Expected bytes as defined by label: " + expectedBytesToRead + + " (" + records + " records times " + length + " bytes per record)" + ", Actual bytes in file: " + + fileSizeMinusOffset + " (" + (fileSizeMinusOffset / length) + " records times " + length + " bytes per record)"); + if ((fileSizeMinusOffset / records) * records == fileSizeMinusOffset) + throw new InvalidTableException( + "Data object is truncated. Expected bytes as defined by label: " + expectedBytesToRead + + " (" + records + " records times " + length + " bytes per record)" + ", Actual bytes in file: " + + fileSizeMinusOffset + " (" + records + " records times " + (fileSizeMinusOffset/records) + " bytes per record)"); throw new InvalidTableException( - "Data object is truncated. Expected bytes as defined by label: " + expectedBytesToRead - + " (" + records + " records)" + ", Actual bytes remaining in file: " - + fileSizeMinusOffset + " (" + (fileSizeMinusOffset / length) + " records)"); + "Data object is truncated. Expected bytes as defined by label: " + expectedBytesToRead + + " (" + records + " records times " + length + " bytes per record)" + ", Actual bytes in file: " + + fileSizeMinusOffset + " (" + ((float)(fileSizeMinusOffset) / (float)length) + " records times " + length + " bytes per record)" + + " OR (" + records + " records times " + ((float)fileSizeMinusOffset / (float)records) + " bytes per record)"); } else { throw new InvalidTableException("Expected to read in " + expectedBytesToRead + " bytes but only " + totalBytesRead + " bytes were read for " + url.toString()); diff --git a/src/main/java/gov/nasa/pds/objectAccess/DelimitedTableRecord.java b/src/main/java/gov/nasa/pds/objectAccess/DelimitedTableRecord.java index ecf210ad..55df8e73 100644 --- a/src/main/java/gov/nasa/pds/objectAccess/DelimitedTableRecord.java +++ b/src/main/java/gov/nasa/pds/objectAccess/DelimitedTableRecord.java @@ -85,6 +85,12 @@ public class DelimitedTableRecord implements TableRecord { setRecordValue(value); } + @Override + public int length() { + int len = 0; + for (String rv : this.recordValue) len += rv.length(); + return len; + } @Override public int findColumn(String name) { checkFieldName(name); diff --git a/src/main/java/gov/nasa/pds/objectAccess/FixedTableRecord.java b/src/main/java/gov/nasa/pds/objectAccess/FixedTableRecord.java index 2968cfce..d25d5eb7 100644 --- a/src/main/java/gov/nasa/pds/objectAccess/FixedTableRecord.java +++ b/src/main/java/gov/nasa/pds/objectAccess/FixedTableRecord.java @@ -106,6 +106,9 @@ public FixedTableRecord(byte[] value, Map map, FieldDescription setRecordValue(value); } + @Override + public int length() { return this.recordBytes.length; } + @Override public int findColumn(String name) { checkFieldName(name);