/*
 * Copyright (c) 2002-2009 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 *  the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author Alex Usun
 */
public class ComponentCodec<T>
        implements DataCodec<T> {

    private final DataReader<T> reader;
    private final DataWriter<T> writer;

    public ComponentCodec(DataReader<T> reader, DataWriter<T> writer) {
        this.reader = reader;
        this.writer = writer;
    }

    public void write(T value, OutputStream out)
            throws IOException {
        if (this.writer == null) {
            throw new IOException("Unsupported operation.");
        }

        this.writer.write(value, out);
    }

    public T read(InputStream in)
            throws IOException {
        if (this.reader == null) {
            throw new IOException("Unsupported operation.");
        }

        return this.reader.read(in);
    }

    @Override
    public String toString() {
        return "ComponentCodec {"
                + "reader=" + this.reader
                + ", writer=" + this.writer
                + '}';
    }
}
