diff --git a/Source/com/drew/lang/SequentialReader.java b/Source/com/drew/lang/SequentialReader.java index f9a3f5a15..efa7e48d2 100644 --- a/Source/com/drew/lang/SequentialReader.java +++ b/Source/com/drew/lang/SequentialReader.java @@ -362,7 +362,9 @@ public StringValue getNullTerminatedStringValue(int maxLengthBytes, Charset char /** * Returns the sequence of bytes punctuated by a \0 value. - * + * It will place the cursor after the first occurrence of \0. + *
+ * Use getNullTerminatedStringAndSkipToNextPosition if you want the cursor to move moved at the end of maxLengthBytes. * @param maxLengthBytes The maximum number of bytes to read. If a \0 byte is not reached within this limit, * the returned array will be maxLengthBytes long. * @return The read byte array, excluding the null terminator. @@ -386,4 +388,20 @@ public byte[] getNullTerminatedBytes(int maxLengthBytes) throws IOException System.arraycopy(buffer, 0, bytes, 0, length); return bytes; } + + + /** + * Read until the null terminated byte and automatically move the end of the requested position. + * @param maxLengthBytes + * @param charset + * @return + * @throws IOException + */ + public StringValue getNullTerminatedStringAndSkipToNextPosition(int maxLengthBytes, Charset charset) throws IOException { + byte[] bytes = this.getNullTerminatedBytes(maxLengthBytes); + if (bytes.length < maxLengthBytes - 1) { + this.trySkip(maxLengthBytes - bytes.length - 1); + } + return new StringValue(bytes, charset); + } } diff --git a/Source/com/drew/metadata/exif/makernotes/NikonPictureControl1Directory.java b/Source/com/drew/metadata/exif/makernotes/NikonPictureControl1Directory.java index 9cf94bfb6..8ec01ba56 100644 --- a/Source/com/drew/metadata/exif/makernotes/NikonPictureControl1Directory.java +++ b/Source/com/drew/metadata/exif/makernotes/NikonPictureControl1Directory.java @@ -78,9 +78,9 @@ public static NikonPictureControl1Directory read(byte[] bytes) throws IOExceptio NikonPictureControl1Directory directory = new NikonPictureControl1Directory(); - directory.setObject(TAG_PICTURE_CONTROL_VERSION, reader.getStringValue(4, Charsets.UTF_8)); - directory.setObject(TAG_PICTURE_CONTROL_NAME, reader.getStringValue(20, Charsets.UTF_8)); - directory.setObject(TAG_PICTURE_CONTROL_BASE, reader.getStringValue(20, Charsets.UTF_8)); + directory.setString(TAG_PICTURE_CONTROL_VERSION, reader.getNullTerminatedStringAndSkipToNextPosition(4, Charsets.UTF_8).toString()); + directory.setString(TAG_PICTURE_CONTROL_NAME, reader.getNullTerminatedStringAndSkipToNextPosition(20, Charsets.UTF_8).toString()); + directory.setString(TAG_PICTURE_CONTROL_BASE, reader.getNullTerminatedStringAndSkipToNextPosition(20, Charsets.UTF_8).toString()); reader.skip(4); directory.setObject(TAG_PICTURE_CONTROL_ADJUST, reader.getUInt8()); directory.setObject(TAG_PICTURE_CONTROL_QUICK_ADJUST, reader.getUInt8()); diff --git a/Source/com/drew/metadata/exif/makernotes/NikonPictureControl2Directory.java b/Source/com/drew/metadata/exif/makernotes/NikonPictureControl2Directory.java index 74cb0a2cd..2c98abff8 100644 --- a/Source/com/drew/metadata/exif/makernotes/NikonPictureControl2Directory.java +++ b/Source/com/drew/metadata/exif/makernotes/NikonPictureControl2Directory.java @@ -86,9 +86,9 @@ public static NikonPictureControl2Directory read(byte[] bytes) throws IOExceptio NikonPictureControl2Directory directory = new NikonPictureControl2Directory(); - directory.setString(TAG_PICTURE_CONTROL_VERSION, reader.getStringValue(4, Charsets.UTF_8).toString()); - directory.setString(TAG_PICTURE_CONTROL_NAME, reader.getStringValue(20, Charsets.UTF_8).toString()); - directory.setString(TAG_PICTURE_CONTROL_BASE, reader.getStringValue(20, Charsets.UTF_8).toString()); + directory.setString(TAG_PICTURE_CONTROL_VERSION, reader.getNullTerminatedStringAndSkipToNextPosition(4, Charsets.UTF_8).toString()); + directory.setString(TAG_PICTURE_CONTROL_NAME, reader.getNullTerminatedStringAndSkipToNextPosition(20, Charsets.UTF_8).toString()); + directory.setString(TAG_PICTURE_CONTROL_BASE, reader.getNullTerminatedStringAndSkipToNextPosition(20, Charsets.UTF_8).toString()); reader.skip(4); directory.setObject(TAG_PICTURE_CONTROL_ADJUST, reader.getByte()); diff --git a/Tests/com/drew/lang/SequentialAccessTestBase.java b/Tests/com/drew/lang/SequentialAccessTestBase.java index ff1a6c7c3..c1dae75ab 100644 --- a/Tests/com/drew/lang/SequentialAccessTestBase.java +++ b/Tests/com/drew/lang/SequentialAccessTestBase.java @@ -241,24 +241,39 @@ public void testGetNullTerminatedString() throws IOException } @Test - public void testGetNullTerminatedStringCursorPositionTest() throws IOException - { + public void testGetNullTerminatedStringCursorPositionTest() throws IOException { byte NULL = 0x00; byte[] bytes = new byte[]{0x41, 0x42, NULL, NULL, NULL, 0x43, 0x44, NULL, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46}; //AB\0\0\0CD\0ABCDEF SequentialReader reader = createReader(bytes); - // try to read first five values - assertEquals("AB", reader.getNullTerminatedString(5, Charsets.UTF_8)); + /* + tried to read first five values + */ + assertEquals("AB", reader.getNullTerminatedString(5, Charsets.UTF_8).toString()); - // the cursor is after B (third) position + /* + the cursor is after B (third) position + */ assertEquals(reader.getPosition(), 3); reader.skip(2); - assertEquals("CD", reader.getNullTerminatedString(3, Charsets.UTF_8)); + assertEquals("CD", reader.getNullTerminatedString(3, Charsets.UTF_8).toString()); assertEquals(reader.getPosition(), 8); //no need to skip to next position. since there's only one \0 character after "CD" - assertEquals("ABCDEF", reader.getNullTerminatedString(6, Charsets.UTF_8)); + + assertEquals("ABCDEF", reader.getNullTerminatedString(6, Charsets.UTF_8).toString()); + } + + @Test + public void testGetNullTerminatedStringAndSkipToNextPosition() throws IOException { + byte NULL = 0x00; + byte[] bytes = new byte[]{0x41, 0x42, NULL, NULL, NULL, 0x43, 0x44, NULL, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46}; + SequentialReader reader = createReader(bytes); + + assertEquals("AB", reader.getNullTerminatedStringAndSkipToNextPosition(5, Charsets.UTF_8).toString()); + assertEquals("CD", reader.getNullTerminatedStringAndSkipToNextPosition(3, Charsets.UTF_8).toString()); + assertEquals("ABCDEF", reader.getNullTerminatedStringAndSkipToNextPosition(6, Charsets.UTF_8).toString()); } @Test