Search in sources :

Example 1 with IonBlob

use of com.amazon.ion.IonBlob in project amazon-qldb-dmv-sample-java by aws-samples.

the class QldbRevision method fromIon.

/**
 * Constructs a new {@link QldbRevision} from an {@link IonStruct}.
 *
 * The specified {@link IonStruct} must include the following fields
 *
 * - blockAddress -- a {@link BlockAddress},
 * - metadata -- a {@link RevisionMetadata},
 * - hash -- the document's hash calculated by QLDB,
 * - data -- an {@link IonStruct} containing user data in the document.
 *
 * If any of these fields are missing or are malformed, then throws {@link IllegalArgumentException}.
 *
 * If the document hash calculated from the members of the specified {@link IonStruct} does not match
 * the hash member of the {@link IonStruct} then throws {@link IllegalArgumentException}.
 *
 * @param ionStruct
 *              The {@link IonStruct} that contains a {@link QldbRevision} object.
 * @return the converted {@link QldbRevision} object.
 * @throws IOException if failed to parse parameter {@link IonStruct}.
 */
public static QldbRevision fromIon(final IonStruct ionStruct) throws IOException {
    try {
        BlockAddress blockAddress = Constants.MAPPER.readValue(ionStruct.get("blockAddress"), BlockAddress.class);
        IonBlob hash = (IonBlob) ionStruct.get("hash");
        IonStruct metadataStruct = (IonStruct) ionStruct.get("metadata");
        IonStruct data = (IonStruct) ionStruct.get("data");
        if (hash == null || data == null) {
            throw new IllegalArgumentException("Document is missing required fields");
        }
        verifyRevisionHash(metadataStruct, data, hash.getBytes());
        RevisionMetadata metadata = RevisionMetadata.fromIon(metadataStruct);
        return new QldbRevision(blockAddress, metadata, hash.getBytes(), data);
    } catch (ClassCastException e) {
        log.error("Failed to parse ion document");
        throw new IllegalArgumentException("Document members are not of the correct type", e);
    }
}
Also used : IonStruct(com.amazon.ion.IonStruct) IonBlob(com.amazon.ion.IonBlob)

Example 2 with IonBlob

use of com.amazon.ion.IonBlob 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 3 with IonBlob

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

the class IonWriterSystemTree method writeBlob.

public void writeBlob(byte[] value, int start, int len) throws IOException {
    IonBlob v = _factory.newBlob(value, start, len);
    append(v);
}
Also used : IonBlob(com.amazon.ion.IonBlob)

Example 4 with IonBlob

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

the class _Private_CurriedValueFactory method newNullBlob.

// -------------------------------------------------------------------------
public IonBlob newNullBlob() {
    IonBlob v = myFactory.newNullBlob();
    handle(v);
    return v;
}
Also used : IonBlob(com.amazon.ion.IonBlob)

Example 5 with IonBlob

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

the class PrinterTest method testPrintingBlob.

@Test
public void testPrintingBlob() throws Exception {
    IonBlob value = system().newNullBlob();
    checkNullRendering("null.blob", value);
    for (int i = 0; i < BlobTest.TEST_DATA.length; i++) {
        BlobTest.TestData td = BlobTest.TEST_DATA[i];
        value.setBytes(td.bytes);
        myPrinter.setPrintBlobAsString(true);
        checkRendering("\"" + td.base64 + "\"", value);
        myPrinter.setPrintBlobAsString(false);
        checkRendering("{{" + td.base64 + "}}", value);
    }
    value = (IonBlob) oneValue("{{}}");
    checkRendering("{{}}", value);
    value.addTypeAnnotation("an");
    checkRendering("an::{{}}", value);
}
Also used : BlobTest(com.amazon.ion.BlobTest) IonBlob(com.amazon.ion.IonBlob) Test(org.junit.Test) IntTest(com.amazon.ion.IntTest) BlobTest(com.amazon.ion.BlobTest) ClobTest(com.amazon.ion.ClobTest)

Aggregations

IonBlob (com.amazon.ion.IonBlob)9 IonStruct (com.amazon.ion.IonStruct)3 IonClob (com.amazon.ion.IonClob)2 IonList (com.amazon.ion.IonList)2 IonSexp (com.amazon.ion.IonSexp)2 IonTimestamp (com.amazon.ion.IonTimestamp)2 Test (org.junit.Test)2 BlobTest (com.amazon.ion.BlobTest)1 ClobTest (com.amazon.ion.ClobTest)1 IntTest (com.amazon.ion.IntTest)1 IonBool (com.amazon.ion.IonBool)1 IonDatagram (com.amazon.ion.IonDatagram)1 IonDecimal (com.amazon.ion.IonDecimal)1 IonException (com.amazon.ion.IonException)1 IonFloat (com.amazon.ion.IonFloat)1 IonInt (com.amazon.ion.IonInt)1 IonLob (com.amazon.ion.IonLob)1 IonNull (com.amazon.ion.IonNull)1 IonString (com.amazon.ion.IonString)1 IonSymbol (com.amazon.ion.IonSymbol)1