Search in sources :

Example 1 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonReaderBinaryIncremental method nextAtTopLevel.

/**
 * Advance the reader to the next top-level value. Buffers an entire top-level value, reads any IVMs and/or local
 * symbol tables that precede the value, and sets the byte positions of important components of the value.
 */
private void nextAtTopLevel() {
    if (completeValueBuffered) {
        // There is already data buffered, but the user is choosing to skip it.
        buffer.seekTo(valueEndPosition);
        completeValueBuffered = false;
    }
    try {
        lookahead.fillInput();
    } catch (Exception e) {
        throw new IonException(e);
    }
    if (lookahead.moreDataRequired()) {
        valueType = null;
        valueTypeID = null;
        return;
    }
    completeValueBuffered = true;
    if (lookahead.getIvmIndex() > -1) {
        peekIndex = lookahead.getIvmIndex();
        majorVersion = buffer.peek(peekIndex++);
        minorVersion = buffer.peek(peekIndex++);
        if (buffer.peek(peekIndex++) != IVM_FINAL_BYTE) {
            throw new IonException("Invalid Ion version marker.");
        }
        requireSupportedIonVersion();
        resetSymbolTable();
        resetImports();
        lookahead.resetIvmIndex();
    } else if (peekIndex < 0) {
        // not represent valid binary Ion, a quick failure is necessary.
        throw new IonException("Binary Ion must start with an Ion version marker.");
    }
    List<IonReaderLookaheadBuffer.Marker> symbolTableMarkers = lookahead.getSymbolTableMarkers();
    if (!symbolTableMarkers.isEmpty()) {
        // The cached SymbolTable (if any) is a snapshot in time, so it must be cleared whenever a new symbol
        // table is read regardless of whether the new LST is an append or a reset.
        cachedReadOnlySymbolTable = null;
        for (IonReaderLookaheadBuffer.Marker symbolTableMarker : symbolTableMarkers) {
            readSymbolTable(symbolTableMarker);
        }
        lookahead.resetSymbolTableMarkers();
    }
    peekIndex = lookahead.getValueStart();
    hasAnnotations = lookahead.hasAnnotations();
    if (hasAnnotations) {
        if (peekIndex >= lookahead.getValueEnd()) {
            throw new IonException("Annotation wrappers without values are invalid.");
        }
        annotationSids.clear();
        IonReaderLookaheadBuffer.Marker annotationSidsMarker = lookahead.getAnnotationSidsMarker();
        annotationStartPosition = annotationSidsMarker.startIndex;
        annotationEndPosition = annotationSidsMarker.endIndex;
        peekIndex = annotationEndPosition;
        valueTypeID = IonTypeID.TYPE_IDS[buffer.peek(peekIndex++)];
        int wrappedValueLength = valueTypeID.length;
        if (valueTypeID.variableLength) {
            wrappedValueLength = readVarUInt();
        }
        valueType = valueTypeID.type;
        if (valueType == IonTypeID.ION_TYPE_ANNOTATION_WRAPPER) {
            throw new IonException("Nested annotations are invalid.");
        }
        if (peekIndex + wrappedValueLength != lookahead.getValueEnd()) {
            throw new IonException("Mismatched annotation wrapper length.");
        }
    } else {
        valueTypeID = lookahead.getValueTid();
        valueType = valueTypeID.type;
    }
    valueStartPosition = peekIndex;
    valueEndPosition = lookahead.getValueEnd();
    lookahead.resetNopPadIndex();
}
Also used : IonException(com.amazon.ion.IonException) ReadOnlyValueException(com.amazon.ion.ReadOnlyValueException) IOException(java.io.IOException) IonException(com.amazon.ion.IonException) UnknownSymbolException(com.amazon.ion.UnknownSymbolException)

Example 2 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonReaderBinaryRawX method readULong.

protected final long readULong(int len) throws IOException {
    long retvalue = 0;
    int b;
    switch(len) {
        default:
            throw new IonException("value too large for Java long");
        case 8:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 7:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 6:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 5:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 4:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 3:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 2:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 1:
            if ((b = read()) < 0)
                throwUnexpectedEOFException();
            retvalue = (retvalue << 8) | b;
        case 0:
    }
    return retvalue;
}
Also used : IonException(com.amazon.ion.IonException) SavePoint(com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)

Example 3 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonJavaCli method validateEvent.

private static void validateEvent(String textValue, byte[] binaryValue, EventType eventType, SymbolToken fieldName, IonType ionType, ImportDescriptor[] imports, int depth) {
    if (eventType == null)
        throw new IllegalStateException("only can convert Struct to Event");
    switch(eventType) {
        case CONTAINER_START:
            if (ionType == null || depth == -1) {
                throw new IonException("Invalid CONTAINER_START: missing field(s)");
            } else if (!IonType.isContainer(ionType)) {
                throw new IonException("Invalid CONTAINER_START: not a container");
            } else if (textValue != null || binaryValue != null) {
                throw new IonException("Invalid CONTAINER_START: value_binary and value_text are only applicable" + " for SCALAR events");
            } else if (imports != null) {
                throw new IonException("Invalid CONTAINER_START: imports must only be present with SYMBOL_TABLE " + "events");
            }
            break;
        case SCALAR:
            if (ionType == null || textValue == null || binaryValue == null || depth == -1) {
                throw new IonException("Invalid SCALAR: missing field(s)");
            } else if (imports != null) {
                throw new IonException("Invalid SCALAR: imports must only be present with SYMBOL_TABLE " + "events");
            }
            // compare text value and binary value
            IonValue text = ION_SYSTEM.singleValue(textValue);
            IonValue binary = ION_SYSTEM.singleValue(binaryValue);
            if (!Equivalence.ionEquals(text, binary)) {
                throw new IonException("invalid Event: Text value and Binary value are different");
            }
            break;
        case SYMBOL_TABLE:
            if (depth == -1) {
                throw new IonException("Invalid SYMBOL_TABLE: missing depth");
            } else if (imports == null) {
                throw new IonException("Invalid SYMBOL_TABLE: missing imports");
            } else if (textValue != null && binaryValue != null) {
                throw new IonException("Invalid SYMBOL_TABLE: text_value and binary_value " + "are only applicable for SCALAR events");
            } else if (fieldName != null && ionType != null) {
                throw new IonException("Invalid SYMBOL_TABLE: unnecessary fields");
            }
            break;
        case CONTAINER_END:
            if (depth == -1 || ionType == null) {
                throw new IonException("Invalid CONTAINER_END: missing depth");
            } else if (textValue != null && binaryValue != null) {
                throw new IonException("Invalid CONTAINER_END: text_value and binary_value " + "are only applicable for SCALAR events");
            } else if (fieldName != null && imports != null) {
                throw new IonException("Invalid CONTAINER_END: unnecessary fields");
            }
            break;
        case STREAM_END:
            if (depth == -1) {
                throw new IonException("Invalid STREAM_END: missing depth");
            } else if (textValue != null && binaryValue != null) {
                throw new IonException("Invalid STREAM_END: text_value and binary_value " + "are only applicable for SCALAR events");
            } else if (fieldName != null && ionType != null && imports != null) {
                throw new IonException("Invalid STREAM_END: unnecessary fields");
            }
            break;
        default:
            throw new IonException("Invalid event_type");
    }
}
Also used : IonValue(com.amazon.ion.IonValue) IonException(com.amazon.ion.IonException)

Example 4 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonJavaCli method ionStreamToEventStream.

private static void ionStreamToEventStream(IonReader ionReader, CommandType commandType, SymbolTable curTable, ReadContext readContext) throws IOException {
    if (ionReader.getType() == null)
        return;
    do {
        if (ionReader.isNullValue()) {
            IonValue value = ION_SYSTEM.newValue(ionReader);
            value.clearTypeAnnotations();
            readContext.getEventStream().add(new Event(EventType.SCALAR, ionReader.getType(), ionReader.getFieldNameSymbol(), ionReader.getTypeAnnotationSymbols(), value, null, ionReader.getDepth()));
            continue;
        }
        if (!isSameSymbolTable(ionReader.getSymbolTable(), curTable)) {
            curTable = ionReader.getSymbolTable();
            ImportDescriptor[] imports = symbolTableToImports(curTable.getImportedTables());
            if (commandType != CommandType.COMPARE) {
                readContext.getEventStream().add(new Event(EventType.SYMBOL_TABLE, null, null, null, null, imports, 0));
            }
        }
        if (isEmbeddedStream(ionReader)) {
            // get current Ion type and depth
            IonType curType = ionReader.getType();
            int curDepth = ionReader.getDepth();
            // write a Container_Start event and step in
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
            ionReader.stepIn();
            while (ionReader.next() != null) {
                if (ionReader.getType() != IonType.STRING) {
                    throw new IonException("Elements of embedded streams sets must be strings.");
                }
                String stream = ionReader.stringValue();
                try (IonReader tempIonReader = IonReaderBuilder.standard().build(stream)) {
                    SymbolTable symbolTable = tempIonReader.getSymbolTable();
                    while (tempIonReader.next() != null) {
                        ionStreamToEventStream(tempIonReader, commandType, symbolTable, readContext);
                    }
                }
                readContext.getEventStream().add(new Event(EventType.STREAM_END, null, null, null, null, null, 0));
            }
            // write a Container_End event and step out
            readContext.getEventStream().add(new Event(EventType.CONTAINER_END, curType, null, null, null, null, curDepth));
            ionReader.stepOut();
        } else if (IonType.isContainer(ionReader.getType())) {
            // get current Ion type and depth
            IonType curType = ionReader.getType();
            int curDepth = ionReader.getDepth();
            // write a Container_Start event and step in
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
            ionReader.stepIn();
            // recursive call
            ionReader.next();
            ionStreamToEventStream(ionReader, commandType, curTable, readContext);
            // write a Container_End event and step out
            readContext.getEventStream().add(new Event(EventType.CONTAINER_END, curType, null, null, null, null, curDepth));
            ionReader.stepOut();
        } else {
            readContext.getEventStream().add(ionStreamToEvent(ionReader));
        }
    } while (ionReader.next() != null);
    ;
}
Also used : IonValue(com.amazon.ion.IonValue) IonType(com.amazon.ion.IonType) IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) Event(com.amazon.tools.events.Event) SymbolTable(com.amazon.ion.SymbolTable) ImportDescriptor(com.amazon.tools.events.ImportDescriptor)

Example 5 with IonException

use of com.amazon.ion.IonException in project ion-java by amzn.

the class IonJavaCli method validateEventStream.

private static void validateEventStream(List<Event> events) {
    if (events.size() == 0)
        return;
    int depth = 0;
    int i = -1;
    while (++i < events.size()) {
        Event event = events.get(i);
        EventType eventType = event.getEventType();
        if (eventType == EventType.CONTAINER_START) {
            depth++;
        } else if (eventType == EventType.CONTAINER_END) {
            if (--depth < 0) {
                throw new IonException("Invalid event stream: Invalid CONTAINER_END event");
            }
        }
        if (isEmbeddedEvent(event)) {
            while (i < events.size()) {
                if (events.get(++i).getEventType() == EventType.CONTAINER_END) {
                    depth--;
                    break;
                }
                while (i < events.size()) {
                    Event aEvent = events.get(i);
                    EventType aEventType = aEvent.getEventType();
                    if (aEventType == EventType.CONTAINER_START) {
                        depth++;
                    } else if (aEventType == EventType.CONTAINER_END) {
                        if (--depth == 0) {
                            throw new IonException("Invalid EventStream: end without STREAM_END event");
                        }
                    } else if (aEventType == EventType.STREAM_END) {
                        if (depth != 1) {
                            throw new IonException("Invalid EventStream: " + "CONTAINER_START and CONTAINER_END not match");
                        }
                        break;
                    }
                    i++;
                }
                if (i == events.size()) {
                    throw new IonException("Invalid EventStream: end without CONTAINER_END event");
                }
            }
        }
    }
    if (depth != 0) {
        throw new IonException("Invalid event stream: CONTAINER_START and CONTAINER_END events doesn't match");
    }
}
Also used : EventType(com.amazon.tools.events.EventType) IonException(com.amazon.ion.IonException) Event(com.amazon.tools.events.Event)

Aggregations

IonException (com.amazon.ion.IonException)66 IOException (java.io.IOException)31 IonReader (com.amazon.ion.IonReader)10 IonType (com.amazon.ion.IonType)9 SymbolToken (com.amazon.ion.SymbolToken)9 IonValue (com.amazon.ion.IonValue)7 Event (com.amazon.tools.events.Event)7 Test (org.junit.Test)6 IonStruct (com.amazon.ion.IonStruct)5 SymbolTable (com.amazon.ion.SymbolTable)5 SavePoint (com.amazon.ion.impl.UnifiedSavePointManagerX.SavePoint)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 EventType (com.amazon.tools.events.EventType)4 IonWriter (com.amazon.ion.IonWriter)3 IonDatagram (com.amazon.ion.IonDatagram)2 IonLob (com.amazon.ion.IonLob)2 Timestamp (com.amazon.ion.Timestamp)2 BufferManager (com.amazon.ion.impl.IonBinary.BufferManager)2 com.amazon.ion.impl._Private_IonWriter (com.amazon.ion.impl._Private_IonWriter)2