org.apache.hadoop.hdfs.util.ByteBufferOutputStream Java Examples

The following examples show how to use org.apache.hadoop.hdfs.util.ByteBufferOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Row.java    From emodb with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures that either the UTF-8 text has been set directly or by indirectly converting the Map contents to JSON.
 */
private void ensureTextSet() {
    if (_text.limit() == 0) {
        checkState(_map != null, "Neither JSON text nor map has been set");
        _text.clear();
        // First try reading the JSON directly into be buffer.
        try {
            JsonHelper.writeJson(new ByteBufferOutputStream(_text), _map);
            // Set the limit and move the position back to zero.
            _text.flip();
        } catch (Exception e) {
            if (Iterables.tryFind(Throwables.getCausalChain(e), Predicates.instanceOf(BufferOverflowException.class)).isPresent()) {
                // Buffer was insufficient.  Allocate a new array and read the bytes into it.
                byte[] utf8 = JsonHelper.asUtf8Bytes(_map);
                _text = ByteBuffer.wrap(utf8);
            } else {
                throw Throwables.propagate(e);
            }
        }
    }
}