Search in sources :

Example 21 with IonException

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

the class _Private_Utils method encode.

// ========================================================================
/**
 * Encodes a String into bytes of a given encoding.
 * <p>
 * This method is preferred to {@link Charset#encode(String)} and
 * {@link String#getBytes(String)} (<em>etc.</em>)
 * since those methods will replace or ignore bad input, and here we throw
 * an exception.
 *
 * @param s the string to encode.
 *
 * @return the encoded string, not null.
 *
 * @throws IonException if there's a {@link CharacterCodingException}.
 */
public static byte[] encode(String s, Charset charset) {
    CharsetEncoder encoder = charset.newEncoder();
    try {
        ByteBuffer buffer = encoder.encode(CharBuffer.wrap(s));
        byte[] bytes = buffer.array();
        // Make another copy iff there's garbage after the limit.
        int limit = buffer.limit();
        if (limit < bytes.length) {
            bytes = copyOf(bytes, limit);
        }
        return bytes;
    } catch (CharacterCodingException e) {
        throw new IonException("Invalid string data", e);
    }
}
Also used : IonException(com.amazon.ion.IonException) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 22 with IonException

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

the class IonLoaderLite method load.

public IonDatagram load(Reader ionText) throws IonException, IOException {
    try {
        IonReader reader = _readerBuilder.build(ionText);
        IonDatagramLite datagram = load_helper(reader);
        return datagram;
    } catch (IonException e) {
        IOException io = e.causeOfType(IOException.class);
        if (io != null)
            throw io;
        throw e;
    }
}
Also used : IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IOException(java.io.IOException)

Example 23 with IonException

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

the class IonLoaderLite method load.

public IonDatagram load(String ionText) throws IonException {
    try {
        IonReader reader = _readerBuilder.build(ionText);
        IonDatagramLite datagram = load_helper(reader);
        return datagram;
    } catch (IOException e) {
        throw new IonException(e);
    }
}
Also used : IonException(com.amazon.ion.IonException) IonReader(com.amazon.ion.IonReader) IOException(java.io.IOException)

Example 24 with IonException

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

the class IonReaderTextRawX method stepOut.

public void stepOut() {
    if (getDepth() < 1) {
        throw new IllegalStateException(IonMessages.CANNOT_STEP_OUT);
    }
    try {
        finish_value(null);
        switch(getContainerType()) {
            case STRUCT:
                if (!_eof)
                    _scanner.skip_over_struct();
                break;
            case LIST:
                if (!_eof)
                    _scanner.skip_over_list();
                break;
            case SEXP:
                if (!_eof)
                    _scanner.skip_over_sexp();
                break;
            case DATAGRAM:
                break;
            default:
                throw new IllegalStateException("Unexpected value type: " + _value_type);
        }
    } catch (IOException e) {
        throw new IonException(e);
    }
    pop_container_state();
    _scanner.tokenIsFinished();
    try {
        finish_value(null);
    } catch (IOException e) {
        throw new IonException(e);
    }
    clear_value();
    if (_debug)
        System.out.println("stepOUT() new depth: " + getDepth());
}
Also used : IonException(com.amazon.ion.IonException) IOException(java.io.IOException)

Example 25 with IonException

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

the class IonReaderTextSystemX method load_lob_contents.

private int load_lob_contents() throws IOException {
    if (_lob_loaded == LOB_STATE.EMPTY) {
        load_lob_save_point();
    }
    if (_lob_loaded == LOB_STATE.READ) {
        long raw_size = _current_value_save_point.length();
        if (raw_size < 0 || raw_size > Integer.MAX_VALUE) {
            load_lob_length_overflow_error(raw_size);
        }
        _lob_bytes = new byte[(int) raw_size];
        try {
            assert (_current_value_save_point_loaded && _current_value_save_point.isDefined());
            _scanner.save_point_activate(_current_value_save_point);
            _lob_actual_len = readBytes(_lob_bytes, 0, (int) raw_size);
            _scanner.save_point_deactivate(_current_value_save_point);
        } catch (IOException e) {
            throw new IonException(e);
        }
        assert (_lob_actual_len <= raw_size);
        _lob_loaded = LOB_STATE.FINISHED;
    }
    assert (_lob_loaded == LOB_STATE.FINISHED);
    return _lob_actual_len;
}
Also used : IonException(com.amazon.ion.IonException) 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