use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonTokenReader method scanIdentifier.
public Type scanIdentifier(int c) throws IOException {
// reset our local value buffer
resetValue();
// we don't have an identifier type any longer, just string
this.t = Type.constSymbol;
// some strings are keywords, most are not
// mostly we'll guess that it's not a keyword
this.keyword = null;
if (!readIdentifierContents(c)) {
// not quoted (true is quoted)
// anything in quotes (even single quotes) is not a keyword
// and here we're not in quoted content (that is handled above)
// so see if it's also a keyword
this.keyword = IonTokenReader.matchKeyword(value, 0, value.length());
if (this.keyword != null) {
if (this.keyword == Type.kwNull) {
c = this.read();
if (c == '.') {
int dot = value.length();
value.append((char) c);
c = this.read();
// +1 is "enough roap" so if there are extra letters at the end of the keyword we keep at least 1
int added = readIdentifierContents(c, IonTokenConstsX.TN_MAX_NAME_LENGTH + 1);
int kw = IonTokenConstsX.keyword(value, dot + 1, dot + added + 1);
switch(kw) {
case IonTokenConstsX.KEYWORD_NULL:
case IonTokenConstsX.KEYWORD_BOOL:
case IonTokenConstsX.KEYWORD_INT:
case IonTokenConstsX.KEYWORD_FLOAT:
case IonTokenConstsX.KEYWORD_DECIMAL:
case IonTokenConstsX.KEYWORD_TIMESTAMP:
case IonTokenConstsX.KEYWORD_SYMBOL:
case IonTokenConstsX.KEYWORD_STRING:
case IonTokenConstsX.KEYWORD_BLOB:
case IonTokenConstsX.KEYWORD_CLOB:
case IonTokenConstsX.KEYWORD_LIST:
case IonTokenConstsX.KEYWORD_SEXP:
case IonTokenConstsX.KEYWORD_STRUCT:
this.keyword = setNullType(value, dot + 1, value.length() - dot - 1);
break;
default:
// not a valid type name - so we have to unread back to the dot
for (int ii = value.length(); ii > dot; ) {
ii--;
char uc = value.charAt(ii);
unread(uc);
}
String message = position() + ": Expected Ion type after 'null.' but found: " + value;
throw new IonException(message);
}
} else {
unread(c);
}
}
this.t = this.keyword;
return this.t;
}
}
// see if we're a user type of a member name
c = this.readIgnoreWhitespace();
if (c != ':') {
unread(c);
} else {
c = read();
if (c != ':') {
unread(c);
this.t = Type.constMemberName;
} else {
this.t = Type.constUserTypeDecl;
}
}
return this.t;
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonReaderBuilderTest method testEnableIncrementalReading.
@Test
public void testEnableIncrementalReading() throws IOException {
IonReaderBuilder builder = IonReaderBuilder.standard();
assertFalse(builder.isIncrementalReadingEnabled());
builder.withIncrementalReadingEnabled(true);
assertTrue(builder.isIncrementalReadingEnabled());
builder.setIncrementalReadingDisabled();
assertFalse(builder.isIncrementalReadingEnabled());
builder.setIncrementalReadingEnabled();
assertTrue(builder.isIncrementalReadingEnabled());
builder.withIncrementalReadingEnabled(false);
assertFalse(builder.isIncrementalReadingEnabled());
ByteArrayOutputStream data = new ByteArrayOutputStream();
data.write(_Private_IonConstants.BINARY_VERSION_MARKER_1_0);
// 5-byte annotation wrapper (incomplete).
data.write(0xE5);
IonReader reader1 = builder.build(data.toByteArray());
try {
reader1.next();
fail();
} catch (IonException e) {
// Expected; this is a non-incremental reader, but a complete value was not available.
}
builder.withIncrementalReadingEnabled(true);
IonReader reader2 = builder.build(data.toByteArray());
assertNull(reader2.next());
IonReader reader3 = builder.build(new ByteArrayInputStream(data.toByteArray()));
assertNull(reader3.next());
}
use of com.amazon.ion.IonException in project ion-hive-serde by amzn.
the class IonHiveSerDe method deserialize.
@Override
public Object deserialize(final Writable blob) throws SerDeException {
final byte[] bytes;
final int length;
// TextFormat which produces Text
if (blob instanceof Text) {
Text text = (Text) blob;
// getBytes returns a reference to the current buffer which is only valid up to length
bytes = text.getBytes();
length = text.getLength();
} else if (blob instanceof BytesWritable) {
BytesWritable bytesWritable = (BytesWritable) blob;
// getBytes returns a reference to the current buffer which is only valid up to length
bytes = bytesWritable.getBytes();
length = bytesWritable.getLength();
} else {
throw new SerDeException("Invalid Writable instance, must be either Text or BytesWritable, was " + blob.getClass());
}
final IonSystem domFactory = ionFactory.getDomFactory();
try (final IonReader reader = ionFactory.newReader(bytes, 0, length)) {
/*
We're using an IonStruct here because:
1. We need a key-value store to carry column values
2. The top-level IonStruct as a context object carries the IonSystem which we use as a ValueFactory in
the callbacks created in PathExtractionConfig
Refer to https://github.com/amzn/ion-hive-serde/issues/61.
*/
IonStruct struct = domFactory.newEmptyStruct();
if (!serDeProperties.pathExtractorCaseSensitivity()) {
struct = new IonStructCaseInsensitiveDecorator(struct);
}
final PathExtractor<IonStruct> pathExtractor = serDeProperties.pathExtractor();
pathExtractor.match(reader, struct);
return struct;
} catch (IonException e) {
// skips if ignoring malformed
if (serDeProperties.getIgnoreMalformed()) {
return null;
}
throw e;
} catch (IOException e) {
throw new SerDeException(e);
}
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonJavaCli method compareEquivs.
private static boolean compareEquivs(CompareContext compareContext) throws IOException {
int i = 0;
int j = 0;
List<Event> eventStreamFirst = compareContext.getEventStreamFirst();
List<Event> eventStreamSecond = compareContext.getEventStreamSecond();
ComparisonType type = compareContext.getType();
while (i < eventStreamFirst.size() && j < eventStreamSecond.size()) {
Event eventFirst = eventStreamFirst.get(i);
Event eventSecond = eventStreamSecond.get(j);
if (eventFirst.getEventType() == EventType.STREAM_END && eventSecond.getEventType() == EventType.STREAM_END) {
break;
} else if (eventFirst.getEventType() == EventType.STREAM_END || eventSecond.getEventType() == EventType.STREAM_END) {
setReportInfo(i, j, "The input streams had a different number of comparison sets.", compareContext);
return type == ComparisonType.NON_EQUIVS;
} else if (!(eventFirst.getIonType() == IonType.LIST || eventFirst.getIonType() == IonType.SEXP) || !(eventSecond.getIonType() == IonType.LIST || eventSecond.getIonType() == IonType.SEXP)) {
throw new IonException("Comparison sets must be lists or s-expressions.");
} else if (isEmbeddedEvent(eventFirst) ^ isEmbeddedEvent(eventSecond)) {
throw new IonException("Embedded streams set expected.");
}
List<Pair> pairsFirst;
List<Pair> pairsSecond;
if (isEmbeddedEvent(eventFirst) && isEmbeddedEvent(eventSecond)) {
pairsFirst = locateEmbeddedStreamBoundaries(eventStreamFirst, i);
pairsSecond = locateEmbeddedStreamBoundaries(eventStreamSecond, j);
} else {
pairsFirst = locateContainerBoundaries(eventStreamFirst, i);
pairsSecond = locateContainerBoundaries(eventStreamSecond, j);
}
i = pairsFirst.size() == 0 ? i + 1 : pairsFirst.get(pairsFirst.size() - 1).right + 1;
j = pairsSecond.size() == 0 ? j + 1 : pairsSecond.get(pairsSecond.size() - 1).right + 1;
for (int m = 0; m < pairsFirst.size(); m++) {
for (int n = 0; n < pairsSecond.size(); n++) {
if (compareContext.getType() == ComparisonType.NON_EQUIVS) {
if (m == n)
continue;
}
Pair pairFirst = pairsFirst.get(m);
Pair pairSecond = pairsSecond.get(n);
if (compare(compareContext, pairFirst.left, pairFirst.right, pairSecond.left, pairSecond.right) ^ (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE)) {
if (type == ComparisonType.NON_EQUIVS) {
setReportInfo(pairFirst.left, pairSecond.left, "Equivalent values in a non-equivs set.", compareContext);
}
return type == ComparisonType.NON_EQUIVS;
}
}
}
i++;
j++;
}
return (type == ComparisonType.EQUIVS || type == ComparisonType.EQUIVS_TIMELINE);
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonJavaCli method processFiles.
//
//
// functions for processing
//
//
private static void processFiles(IonWriter ionWriterForErrorReport, CommandArgs args, ProcessContext processContext) throws IOException {
boolean finish = false;
for (String path : args.getInputFiles()) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
IonReader ionReader = IonReaderBuilder.standard().build(inputStream)) {
processContext.setFileName(path);
ReadContext readContext = new ReadContext(new ArrayList<>());
try {
getEventStream(ionReader, CommandType.PROCESS, readContext);
} catch (IonException | NullPointerException e) {
new ErrorDescription(readContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
finish = true;
} catch (Exception e) {
new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
finish = true;
}
processContext.setEventStream(readContext.getEventStream());
processContext.setEventIndex(0);
if (args.getOutputFormat() == OutputFormat.EVENTS) {
processContext.getIonWriter().writeSymbol(EVENT_STREAM);
processToEventStream(ionWriterForErrorReport, processContext);
} else {
processToIonStream(processContext, args);
}
if (finish)
System.exit(IO_ERROR_EXIT_CODE);
processContext.getIonWriter().finish();
ionWriterForErrorReport.finish();
} catch (IonException | NullPointerException e) {
new ErrorDescription(processContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
System.exit(IO_ERROR_EXIT_CODE);
} catch (Exception e) {
new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
System.exit(IO_ERROR_EXIT_CODE);
}
}
}
Aggregations