org.fastmongo.odm.bson.repository.BsonDbDecoder.java Source code

Java tutorial

Introduction

Here is the source code for org.fastmongo.odm.bson.repository.BsonDbDecoder.java

Source

/*
 * Copyright (c) 2014 Alexander Gulko <kirhog at gmail dot com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.fastmongo.odm.bson.repository;

import com.mongodb.DBCallback;
import com.mongodb.DBCollection;
import com.mongodb.DBDecoder;
import com.mongodb.DBObject;
import com.mongodb.DefaultDBDecoder;
import org.bson.BSONCallback;
import org.bson.BSONObject;
import org.bson.io.Bits;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Decodes response from mongo to BsonDbObject.
 *
 * @author Alexander Gulko
 */
class BsonDbDecoder implements DBDecoder {
    static final BsonDbDecoder INSTANCE = new BsonDbDecoder();

    private static final int INT_SIZE = Integer.SIZE / Byte.SIZE;

    private BsonDbDecoder() {
    }

    @Override
    public DBObject decode(byte[] b, DBCollection collection) {
        try {
            return decode(new ByteArrayInputStream(b), collection);
        } catch (IOException e) {
            throw new RuntimeException("Can't decode object for collection " + collection);
        }
    }

    @Override
    public DBObject decode(InputStream in, DBCollection collection) throws IOException {
        if (BsonContextHolder.isUseBson()) {
            return decodeToBson(in);
        } else {
            return getDefaultDbDecoder().decode(in, collection);
        }
    }

    private DBDecoder getDefaultDbDecoder() {
        return DefaultDBDecoder.FACTORY.create();
    }

    @Override
    public DBCallback getDBCallback(DBCollection collection) {
        throw new UnsupportedOperationException();
    }

    @Override
    public BSONObject readObject(byte[] b) {
        throw new UnsupportedOperationException();
    }

    @Override
    public BSONObject readObject(InputStream in) throws IOException {
        throw new UnsupportedOperationException();
    }

    @Override
    public int decode(byte[] b, BSONCallback callback) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int decode(InputStream in, BSONCallback callback) throws IOException {
        throw new UnsupportedOperationException();
    }

    private DBObject decodeToBson(InputStream in) throws IOException {
        byte[] data = readElement(in);
        return new BsonDbObject(data);
    }

    private byte[] readElement(InputStream input) throws IOException {
        byte[] sizeBytes = new byte[INT_SIZE];
        fullRead(input, sizeBytes, 0, INT_SIZE);
        int size = Bits.readInt(sizeBytes);

        byte[] data = new byte[size];
        System.arraycopy(sizeBytes, 0, data, 0, INT_SIZE);
        fullRead(input, data, INT_SIZE, size - INT_SIZE);

        return data;
    }

    private void fullRead(InputStream input, byte[] data, int offset, int length) throws IOException {
        int bytesRead;
        while (length > 0) {
            bytesRead = input.read(data, offset, length);
            if (bytesRead < 0) {
                throw new EOFException("Unexpected end of stream");
            }

            offset += bytesRead;
            length -= bytesRead;
        }
    }
}