Search in sources :

Example 26 with IonException

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

the class IonReaderTextSystemX method byteSize.

// 
// blob and clob support routines
// 
public int byteSize() {
    ensureLob("byteSize");
    long len;
    try {
        len = load_lob_contents();
    } catch (IOException e) {
        throw new IonException(e);
    }
    if (len < 0 || len > Integer.MAX_VALUE) {
        load_lob_length_overflow_error(len);
    }
    return (int) len;
}
Also used : IonException(com.amazon.ion.IonException) IOException(java.io.IOException)

Example 27 with IonException

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

the class IonReaderTextSystemX method getIonValue.

public IonValue getIonValue(IonSystem sys) {
    if (isNullValue()) {
        switch(_value_type) {
            case NULL:
                return sys.newNull();
            case BOOL:
                return sys.newNullBool();
            case INT:
                return sys.newNullInt();
            case FLOAT:
                return sys.newNullFloat();
            case DECIMAL:
                return sys.newNullDecimal();
            case TIMESTAMP:
                return sys.newNullTimestamp();
            case SYMBOL:
                return sys.newNullSymbol();
            case STRING:
                return sys.newNullString();
            case CLOB:
                return sys.newNullClob();
            case BLOB:
                return sys.newNullBlob();
            case LIST:
                return sys.newNullList();
            case SEXP:
                return sys.newNullSexp();
            case STRUCT:
                return sys.newNullString();
            default:
                throw new IonException("unrecognized type encountered");
        }
    }
    switch(_value_type) {
        case NULL:
            return sys.newNull();
        case BOOL:
            return sys.newBool(booleanValue());
        case INT:
            return sys.newInt(longValue());
        case FLOAT:
            return sys.newFloat(doubleValue());
        case DECIMAL:
            return sys.newDecimal(decimalValue());
        case TIMESTAMP:
            IonTimestamp t = sys.newNullTimestamp();
            Timestamp ti = timestampValue();
            t.setValue(ti);
            return t;
        case SYMBOL:
            return sys.newSymbol(stringValue());
        case STRING:
            return sys.newString(stringValue());
        case CLOB:
            IonClob clob = sys.newNullClob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            clob.setBytes(newBytes());
            return clob;
        case BLOB:
            IonBlob blob = sys.newNullBlob();
            // FIXME inefficient: both newBytes and setBytes copy the data
            blob.setBytes(newBytes());
            return blob;
        case LIST:
            IonList list = sys.newNullList();
            fillContainerList(sys, list);
            return list;
        case SEXP:
            IonSexp sexp = sys.newNullSexp();
            fillContainerList(sys, sexp);
            return sexp;
        case STRUCT:
            IonStruct struct = sys.newNullStruct();
            fillContainerStruct(sys, struct);
            return struct;
        default:
            throw new IonException("unrecognized type encountered");
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonSexp(com.amazon.ion.IonSexp) IonException(com.amazon.ion.IonException) IonTimestamp(com.amazon.ion.IonTimestamp) IonList(com.amazon.ion.IonList) IonClob(com.amazon.ion.IonClob) IonBlob(com.amazon.ion.IonBlob) Timestamp(com.amazon.ion.Timestamp) IonTimestamp(com.amazon.ion.IonTimestamp)

Example 28 with IonException

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

the class IonReaderTextSystemX method getBytes.

public int getBytes(byte[] buffer, int offset, int len) {
    ensureLob("getBytes");
    if (_lob_loaded == LOB_STATE.READ) {
        // data to something useful
        try {
            load_lob_contents();
        } catch (IOException e) {
            throw new IonException(e);
        }
    }
    int len_read;
    if (_lob_loaded == LOB_STATE.FINISHED) {
        // if we have loaded data, just copy it
        len_read = len;
        if (len_read > _lob_actual_len) {
            len_read = _lob_actual_len;
        }
        System.arraycopy(_lob_bytes, 0, buffer, offset, len_read);
    } else {
        // the input source
        try {
            if (_current_value_save_point_loaded && _lob_value_position > 0) {
                if (_current_value_save_point.isActive()) {
                    _scanner.save_point_deactivate(_current_value_save_point);
                }
                _scanner.save_point_activate(_current_value_save_point);
                _lob_value_position = 0;
            }
            assert (_current_value_save_point_loaded && _current_value_save_point.isDefined());
            _scanner.save_point_activate(_current_value_save_point);
            len_read = readBytes(buffer, offset, len);
            _scanner.save_point_deactivate(_current_value_save_point);
        } catch (IOException e) {
            throw new IonException(e);
        }
    }
    return len_read;
}
Also used : IonException(com.amazon.ion.IonException) IOException(java.io.IOException)

Example 29 with IonException

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

the class IonReaderTreeSystem method newBytes.

public byte[] newBytes() {
    if (_curr instanceof IonLob) {
        IonLob lob = (IonLob) _curr;
        int loblen = lob.byteSize();
        byte[] buffer = new byte[loblen];
        InputStream is = lob.newInputStream();
        int retlen;
        try {
            retlen = readFully(is, buffer, 0, loblen);
            is.close();
        } catch (IOException e) {
            throw new IonException(e);
        }
        assert (retlen == -1 ? loblen == 0 : retlen == loblen);
        return buffer;
    }
    throw new IllegalStateException("current value is not an ion blob or clob");
}
Also used : IonException(com.amazon.ion.IonException) IonLob(com.amazon.ion.IonLob) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 30 with IonException

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

the class IonReaderTreeSystem method getBytes.

public int getBytes(byte[] buffer, int offset, int len) {
    if (_curr instanceof IonLob) {
        IonLob lob = (IonLob) _curr;
        int loblen = lob.byteSize();
        if (loblen > len) {
            throw new IllegalArgumentException("insufficient space in buffer for this value");
        }
        InputStream is = lob.newInputStream();
        int retlen;
        try {
            retlen = readFully(is, buffer, 0, loblen);
            is.close();
        } catch (IOException e) {
            throw new IonException(e);
        }
        assert retlen == loblen;
        return retlen;
    }
    throw new IllegalStateException("current value is not an ion blob or clob");
}
Also used : IonException(com.amazon.ion.IonException) IonLob(com.amazon.ion.IonLob) InputStream(java.io.InputStream) IOException(java.io.IOException)

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