Skip to content

Commit 1c6f20e

Browse files
zvorygindlurton
authored andcommitted
Add IonSystem#singleValue(byte[], int, int) (#257)
When ion object resides in the middle of bytearray, this helper method would help to extract it without memory copy overhead.
1 parent 157d474 commit 1c6f20e

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/com/amazon/ion/IonSystem.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,27 @@ public SymbolTable newSharedSymbolTable(String name,
373373
*/
374374
public IonValue singleValue(byte[] ionData);
375375

376+
/**
377+
* Extracts a single value from Ion text or binary data.
378+
* <p>
379+
* This method will auto-detect and uncompress GZIPped Ion data.
380+
*
381+
* @param ionData is used only within the range of bytes starting at
382+
* {@code offset} for {@code len} bytes.
383+
* The data in that range may be either Ion binary data, or UTF-8 Ion text.
384+
* @param offset must be non-negative and less than {@code ionData.length}.
385+
* @param len must be non-negative and {@code offset+len} must not exceed
386+
*
387+
* @return the first (and only) user value in the data; not null.
388+
*
389+
* @throws NullPointerException if {@code ionData} is null.
390+
* @throws UnexpectedEofException if the data doesn't contain any user
391+
* values.
392+
* @throws IonException if the data does not contain exactly one user
393+
* value.
394+
*/
395+
public IonValue singleValue(byte[] ionData, int offset, int len);
396+
376397

377398
//-------------------------------------------------------------------------
378399
// IonReader creation

src/com/amazon/ion/impl/lite/IonSystemLite.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,12 @@ public IonValue singleValue(String ionText)
523523

524524
public IonValue singleValue(byte[] ionData)
525525
{
526-
IonReader reader = newReader(ionData);
526+
return singleValue(ionData, 0, ionData.length);
527+
}
528+
529+
@Override
530+
public IonValue singleValue(byte[] ionData, int offset, int len) {
531+
IonReader reader = newReader(ionData, offset, len);
527532
try {
528533
Iterator<IonValue> it = iterate(reader);
529534
return singleValue(it);

test/com/amazon/ion/LoaderTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.io.StringReader;
3333
import java.util.Arrays;
3434
import java.util.Iterator;
35+
36+
import org.junit.Assert;
3537
import org.junit.Test;
3638

3739

@@ -393,6 +395,53 @@ public void testSingleValue()
393395
catch (IonException ie) { /* ok */ }
394396
}
395397

398+
@Test
399+
public void testSubBufferSingleValue()
400+
{
401+
IonSystem sys = system();
402+
403+
String image = "(this is a single sexp)";
404+
byte[] bytes = sys.newLoader().load(image).getBytes();
405+
406+
IonValue singleValue = sys.singleValue(bytes, 0, bytes.length);
407+
assertEquals(singleValue.toString(), image);
408+
409+
byte[] padded = new byte[bytes.length + 20];
410+
System.arraycopy(bytes, 0, padded, 10, bytes.length);
411+
412+
IonValue paddedValue = sys.singleValue(padded, 10, bytes.length);
413+
assertEquals(paddedValue.toString(), image);
414+
415+
bytes = sys.newLoader().load("(two) (values)").getBytes();
416+
417+
try {
418+
sys.singleValue(bytes, 0, bytes.length);
419+
fail("Expected IonException");
420+
}
421+
catch (IonException ie) {
422+
Assert.assertTrue(ie.getMessage().contains("not a single value"));
423+
}
424+
425+
padded = new byte[bytes.length + 20];
426+
System.arraycopy(bytes, 0, padded, 10, bytes.length);
427+
428+
try {
429+
sys.singleValue(padded, 10, bytes.length);
430+
fail("Expected IonException");
431+
}
432+
catch (IonException ie) {
433+
Assert.assertTrue(ie.getMessage().contains("not a single value"));
434+
}
435+
436+
try {
437+
sys.singleValue(padded, 15, bytes.length);
438+
fail("Expected IonException");
439+
}
440+
catch (IonException ie) {
441+
Assert.assertTrue(ie.getMessage().contains("bad character"));
442+
}
443+
}
444+
396445
@Test
397446
public void testCatalogOnLoader() throws Exception {
398447
IonSystem sys = newSystem(Symtabs.CATALOG);

0 commit comments

Comments
 (0)