All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backblaze.b2.json.B2JsonByteArrayHandler Maven / Gradle / Ivy

Go to download

The core logic for B2 SDK for Java. Does not include any implementations of B2WebApiClient.

There is a newer version: 6.3.0
Show newest version
/*
 * Copyright 2017, Backblaze Inc. All Rights Reserved.
 * License https://www.backblaze.com/using_b2_code.html
 */

package com.backblaze.b2.json;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class B2JsonByteArrayHandler extends B2JsonNonUrlTypeHandler {

    private final B2JsonTypeHandler itemHandler;

    public B2JsonByteArrayHandler(B2JsonTypeHandler itemHandler) {
        this.itemHandler = itemHandler;
    }

    public Class getHandledClass() {
        return byte[].class;
    }

    public void serialize(byte[] array, B2JsonWriter out) throws IOException, B2JsonException {
        out.setAllowNewlines(false);
        out.startArray();
        for (Byte item : array) {
            out.startArrayValue();
            //noinspection unchecked
            B2JsonUtil.serializeMaybeNull(itemHandler, item, out);
        }
        out.finishArray();
        out.setAllowNewlines(true);
    }

    public byte[] deserialize(B2JsonReader in, int options) throws B2JsonException, IOException {
        List result = new ArrayList<>();
        if (in.startArrayAndCheckForContents()) {
            do {
                result.add((Byte) B2JsonUtil.deserializeMaybeNull(itemHandler, in, options));
            } while (in.arrayHasMoreValues());
        }
        in.finishArray();

        final int nElts = result.size();
        final byte[] array = new byte[nElts];
        int i = 0;
        for (Byte elt : result) {
            if (elt == null) {
                throw new B2JsonBadValueException("can't put null in a byte[].");
            }
            array[i] = elt;
            i++;
        }
        return array;
    }

    public byte[] defaultValueForOptional() {
        return null;
    }

    public boolean isStringInJson() {
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy