use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonJavaCli method parseStruct.
private static IonStruct parseStruct(ContainerContext containerContext, CompareContext compareContext, int end, boolean isFirst) {
Event initEvent = isFirst ? compareContext.getEventStreamFirst().get(containerContext.getIndex()) : compareContext.getEventStreamSecond().get(containerContext.getIndex());
if (initEvent.getEventType() != EventType.CONTAINER_START || initEvent.getIonType() != IonType.STRUCT) {
return null;
}
IonStruct ionStruct = ION_SYSTEM.newEmptyStruct();
while (containerContext.increaseIndex() < end) {
Event event = isFirst ? compareContext.getEventStreamFirst().get(containerContext.getIndex()) : compareContext.getEventStreamSecond().get(containerContext.getIndex());
EventType eventType = event.getEventType();
if (eventType == EventType.CONTAINER_START) {
switch(event.getIonType()) {
case LIST:
ionStruct.add(event.getFieldName(), parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptyList()));
break;
case STRUCT:
ionStruct.add(event.getFieldName(), parseStruct(containerContext, compareContext, end, isFirst));
break;
case SEXP:
ionStruct.add(event.getFieldName(), parseSequence(containerContext, compareContext, end, isFirst, ION_SYSTEM.newEmptySexp()));
break;
}
} else if (eventType == EventType.CONTAINER_END) {
break;
} else if (eventType == EventType.STREAM_END) {
throw new IonException("Invalid struct: eventStream ends without CONTAINER_END");
} else {
IonValue cloneValue = event.getValue().clone();
cloneValue.setTypeAnnotationSymbols(event.getAnnotations());
ionStruct.add(event.getFieldName(), cloneValue);
}
}
return ionStruct;
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonBinary method verifyBinaryVersionMarker.
/**
* Verifies that a reader starts with a valid Ion cookie, throwing an
* exception if it does not.
*
* @param reader must not be null.
* @throws IonException if there's a problem reading the cookie, or if the
* data does not start with {@link _Private_IonConstants#BINARY_VERSION_MARKER_1_0}.
*/
public static void verifyBinaryVersionMarker(Reader reader) throws IonException {
try {
int pos = reader.position();
// reader.sync();
// reader.setPosition(0);
byte[] bvm = new byte[BINARY_VERSION_MARKER_SIZE];
int len = readFully(reader, bvm);
if (len < BINARY_VERSION_MARKER_SIZE) {
String message = "Binary data is too short: at least " + BINARY_VERSION_MARKER_SIZE + " bytes are required, but only " + len + " were found.";
throw new IonException(message);
}
if (!isIonBinary(bvm)) {
StringBuilder buf = new StringBuilder();
buf.append("Binary data has unrecognized header");
for (int i = 0; i < bvm.length; i++) {
int b = bvm[i] & 0xFF;
buf.append(" 0x");
buf.append(Integer.toHexString(b).toUpperCase());
}
throw new IonException(buf.toString());
}
// reader.setPosition(0); // cas 19 apr 2008
// cas 5 may 2009 :)
reader.setPosition(pos);
} catch (IOException e) {
throw new IonException(e);
}
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonReaderBinaryIncremental method getSymbolToken.
/**
* Creates a SymbolToken representation of the given symbol ID.
* @param sid a symbol ID.
* @return a SymbolToken.
*/
private SymbolToken getSymbolToken(int sid) {
int symbolTableSize = maxSymbolId() + 1;
if (symbolTokensById == null) {
symbolTokensById = new ArrayList<SymbolToken>(symbolTableSize);
}
if (symbolTokensById.size() < symbolTableSize) {
for (int i = symbolTokensById.size(); i < symbolTableSize; i++) {
symbolTokensById.add(null);
}
}
if (sid >= symbolTableSize) {
throw new IonException("Symbol ID exceeds the max ID of the symbol table.");
}
SymbolToken token = symbolTokensById.get(sid);
if (token == null) {
String text = getSymbolString(sid, imports, symbols);
ImportLocation importLocation = null;
if (text == null) {
// Note: this will never be a system symbol.
if (sid > 0 && sid <= imports.getMaxId()) {
importLocation = imports.getImportLocation(sid);
} else {
// All symbols with unknown text in the local symbol range are equivalent to symbol zero.
sid = 0;
}
}
token = new SymbolTokenImpl(text, sid, importLocation);
symbolTokensById.set(sid, token);
}
return token;
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonReaderLookaheadBuffer method readTypeID.
/**
* Reads the type ID byte.
* @param isUnannotated true if this type ID is not on a value within an annotation wrapper; false if it is.
* @return the result as a {@link ReadTypeIdResult}.
* @throws Exception if thrown by a handler method or if an IOException is thrown by the underlying InputStream.
*/
private ReadTypeIdResult readTypeID(final boolean isUnannotated) throws Exception {
int header = readByte();
if (header < 0) {
return ReadTypeIdResult.NO_DATA;
}
valueTid = IonTypeID.TYPE_IDS[header];
dataHandler.onData(1);
if (header == IVM_START_BYTE) {
if (!isUnannotated) {
throw new IonException("Invalid annotation header.");
}
additionalBytesNeeded = IVM_REMAINING_LENGTH;
isSystemValue = true;
// Encountering an IVM resets the symbol table context; no need to parse any previous symbol tables.
resetSymbolTableMarkers();
ivmSecondByteIndex = peekIndex;
state = State.SKIPPING_VALUE;
} else if (!valueTid.isValid) {
throw new IonException("Invalid type ID.");
} else if (valueTid.type == IonType.BOOL) {
// bool values are always a single byte.
state = State.BEFORE_TYPE_ID;
} else if (valueTid.type == IonTypeID.ION_TYPE_ANNOTATION_WRAPPER) {
// Annotation.
if (valueTid.variableLength) {
initializeVarUInt(VarUInt.Location.ANNOTATION_WRAPPER_LENGTH);
} else {
setAdditionalBytesNeeded(valueTid.length, isUnannotated);
initializeVarUInt(VarUInt.Location.ANNOTATION_WRAPPER_SIDS_LENGTH);
}
} else {
if (valueTid.isNull) {
// null values are always a single byte.
state = State.BEFORE_TYPE_ID;
} else {
// Not null
if (valueTid.variableLength) {
initializeVarUInt(VarUInt.Location.VALUE_LENGTH);
} else {
setAdditionalBytesNeeded(valueTid.length, isUnannotated);
state = State.SKIPPING_VALUE;
}
}
}
if (valueTid.type == IonType.STRUCT) {
return ReadTypeIdResult.STRUCT;
}
return ReadTypeIdResult.NOT_STRUCT;
}
use of com.amazon.ion.IonException in project ion-java by amzn.
the class IonReaderBinaryIncremental method readSymbolTable.
/**
* Reads a local symbol table from the buffer.
* @param marker marker for the start and end positions of the local symbol table in the buffer.
*/
private void readSymbolTable(IonReaderLookaheadBuffer.Marker marker) {
peekIndex = marker.startIndex;
boolean isAppend = false;
boolean hasSeenImports = false;
boolean hasSeenSymbols = false;
int symbolsPosition = -1;
int symbolsEndPosition = -1;
List<SymbolTable> newImports;
while (peekIndex < marker.endIndex) {
fieldNameSid = readVarUInt();
IonTypeID typeID = readTypeId();
calculateEndPosition(typeID);
int currentValueEndPosition = valueEndPosition;
if (fieldNameSid == SystemSymbolIDs.IMPORTS_ID) {
if (hasSeenImports) {
throw new IonException("Symbol table contained multiple imports fields.");
}
if (typeID.type == IonType.SYMBOL) {
isAppend = readUInt(peekIndex, currentValueEndPosition) == SystemSymbolIDs.ION_SYMBOL_TABLE_ID;
peekIndex = currentValueEndPosition;
} else if (typeID.type == IonType.LIST) {
resetImports();
newImports = new ArrayList<SymbolTable>(3);
newImports.add(getSystemSymbolTable());
stepIn();
IonType type = next();
while (type != null) {
String name = null;
int version = -1;
int maxId = -1;
if (type == IonType.STRUCT) {
stepIn();
type = next();
while (type != null) {
int fieldSid = getFieldId();
if (fieldSid == SystemSymbolIDs.NAME_ID) {
if (type == IonType.STRING) {
name = stringValue();
}
} else if (fieldSid == SystemSymbolIDs.VERSION_ID) {
if (type == IonType.INT) {
version = intValue();
}
} else if (fieldSid == SystemSymbolIDs.MAX_ID_ID) {
if (type == IonType.INT) {
maxId = intValue();
}
}
type = next();
}
stepOut();
}
newImports.add(createImport(name, version, maxId));
type = next();
}
stepOut();
imports = new LocalSymbolTableImports(newImports);
}
if (!isAppend) {
// Clear the existing symbols before adding the new imported symbols.
resetSymbolTable();
}
hasSeenImports = true;
} else if (fieldNameSid == SystemSymbolIDs.SYMBOLS_ID) {
if (hasSeenSymbols) {
throw new IonException("Symbol table contained multiple symbols fields.");
}
if (typeID.type == IonType.LIST) {
// Just record this position and skip forward. Come back after the imports (if any) are parsed.
symbolsPosition = peekIndex;
symbolsEndPosition = currentValueEndPosition;
}
hasSeenSymbols = true;
}
peekIndex = currentValueEndPosition;
}
if (!hasSeenImports) {
resetSymbolTable();
resetImports();
}
if (symbolsPosition > -1) {
peekIndex = symbolsPosition;
valueType = IonType.LIST;
valueEndPosition = symbolsEndPosition;
stepIn();
while (next() != null) {
if (valueType != IonType.STRING) {
symbols.add(null);
} else {
symbols.add(stringValue());
}
}
stepOut();
peekIndex = valueEndPosition;
}
}
Aggregations