/**
 * ****************************************************************************
 * Copyright 2013 EMBL-EBI
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 htsjdk.samtools.cram.encoding.external;

import htsjdk.samtools.cram.encoding.CRAMCodec;
import htsjdk.samtools.cram.io.CRAMByteReader;
import htsjdk.samtools.cram.io.CRAMByteWriter;
import htsjdk.samtools.cram.io.ITF8;
import htsjdk.samtools.cram.structure.SliceBlocksReadStreams;
import htsjdk.samtools.cram.structure.SliceBlocksWriteStreams;

public final class ExternalByteArrayEncoding extends ExternalEncoding<byte[]> {

    public ExternalByteArrayEncoding(final int externalBlockContentId) {
        super(externalBlockContentId);
    }

    /**
     * Create a new instance of this encoding using the (ITF8 encoded) serializedParams.
     * @param serializedParams
     * @return ExternalByteArrayEncoding with parameters populated from serializedParams
     */
    public static ExternalByteArrayEncoding fromSerializedEncodingParams(byte[] serializedParams) {
        final int contentId = ITF8.readUnsignedITF8(serializedParams);
        return new ExternalByteArrayEncoding(contentId);
    }

    @Override
    public byte[] toSerializedEncodingParams() {
        return ITF8.writeUnsignedITF8(externalBlockContentId);
    }

    @Override
    public CRAMCodec<byte[]> buildCodec(
            final SliceBlocksReadStreams sliceBlocksReadStreams,
            final SliceBlocksWriteStreams sliceBlocksWriteStreams) {
        final CRAMByteReader reader = sliceBlocksReadStreams == null
                ? null
                : sliceBlocksReadStreams.getExternalReader(externalBlockContentId);
        final CRAMByteWriter writer = sliceBlocksWriteStreams == null
                ? null
                : sliceBlocksWriteStreams.getExternalWriter(externalBlockContentId);
        return new ExternalByteArrayCodec(reader, writer);
    }

    @Override
    public String toString() {
        return String.format("Content ID: %d", externalBlockContentId);
    }
}
