com.torodb.torod.db.backends.mysql.converters.array.BinaryToArrayConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.torodb.torod.db.backends.mysql.converters.array.BinaryToArrayConverter.java

Source

/*
 *     This file is part of ToroDB.
 *
 *     ToroDB is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     ToroDB is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with ToroDB. If not, see <http://www.gnu.org/licenses/>.
 *
 *     Copyright (c) 2014, 8Kdata Technology
 *     
 */

package com.torodb.torod.db.backends.mysql.converters.array;

import javax.json.JsonString;
import javax.xml.bind.DatatypeConverter;

import com.google.common.io.ByteSource;
import com.torodb.kvdocument.values.KVBinary.KVBinarySubtype;
import com.torodb.torod.core.exceptions.ToroImplementationException;
import com.torodb.torod.core.subdocument.values.ScalarBinary;
import com.torodb.torod.core.subdocument.values.heap.ByteSourceScalarBinary;
import com.torodb.torod.db.backends.converters.array.ArrayConverter;

/**
 *
 */
public class BinaryToArrayConverter implements ArrayConverter<JsonString, ScalarBinary> {
    private static final long serialVersionUID = 1L;

    @Override
    public String toJsonLiteral(ScalarBinary value) {
        return "base64:type15:" + DatatypeConverter.printBase64Binary(value.getByteSource().read());
    }

    @Override
    public ScalarBinary fromJsonValue(JsonString value) {
        if (!value.getString().startsWith("base64:type15:")) {
            throw new ToroImplementationException(
                    "A binary in escape format was expected, but " + value + " was found");
        }
        return new ByteSourceScalarBinary(KVBinarySubtype.MONGO_GENERIC, (byte) 0,
                ByteSource.wrap(DatatypeConverter.parseBase64Binary(value.getString().substring(14))));
    }
}