com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java Source code

Java tutorial

Introduction

Here is the source code for com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

Source

/*
 * Copyright 2010-2012 Ning, Inc.
 *
 * Ning licenses this file to you 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 com.ning.arecibo.util.timeline.times;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.codec.binary.Hex;

import com.ning.arecibo.util.timeline.chunks.TimeBytesAndSampleBytes;
import com.ning.arecibo.util.timeline.chunks.TimelineChunk;

public class TimesAndSamplesCoder {

    public static int getSizeOfTimeBytes(final byte[] timesAndSamples) {
        final DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(timesAndSamples));
        try {
            return inputStream.readInt();
        } catch (IOException e) {
            throw new IllegalStateException(String.format(
                    "Exception reading timeByteCount in TimelineChunkMapper.map() for timesAndSamples %s",
                    Hex.encodeHex(timesAndSamples)), e);
        }
    }

    public static int getEncodedLength(final TimelineChunk chunk) {
        return 4 + chunk.getTimes().length + chunk.getSamples().length;
    }

    public static byte[] getTimeBytes(final byte[] timesAndSamples) {
        final int timeByteCount = getSizeOfTimeBytes(timesAndSamples);
        return Arrays.copyOfRange(timesAndSamples, 4, 4 + timeByteCount);
    }

    public static byte[] getSampleBytes(final byte[] timesAndSamples) {
        final int timeByteCount = getSizeOfTimeBytes(timesAndSamples);
        return Arrays.copyOfRange(timesAndSamples, 4 + timeByteCount, timesAndSamples.length);
    }

    public static TimeBytesAndSampleBytes getTimesBytesAndSampleBytes(final byte[] timesAndSamples) {
        final int timeByteCount = getSizeOfTimeBytes(timesAndSamples);
        final byte[] timeBytes = Arrays.copyOfRange(timesAndSamples, 4, 4 + timeByteCount);
        final byte[] sampleBytes = Arrays.copyOfRange(timesAndSamples, 4 + timeByteCount, timesAndSamples.length);
        return new TimeBytesAndSampleBytes(timeBytes, sampleBytes);
    }

    public static byte[] combineTimesAndSamples(final byte[] times, final byte[] samples) {
        final int totalSamplesSize = 4 + times.length + samples.length;
        final ByteArrayOutputStream baStream = new ByteArrayOutputStream(totalSamplesSize);
        final DataOutputStream outputStream = new DataOutputStream(baStream);
        try {
            outputStream.writeInt(times.length);
            outputStream.write(times);
            outputStream.write(samples);
            outputStream.flush();
            return baStream.toByteArray();
        } catch (IOException e) {
            throw new IllegalStateException(String.format(
                    "Exception reading timeByteCount in TimelineChunkMapper.map() for times %s, samples %s",
                    Hex.encodeHex(times), Hex.encodeHex(samples)), e);
        }
    }
}