/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.marshalling;

import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectInputValidation;
import java.io.NotActiveException;
import java.io.InvalidObjectException;
import java.io.ObjectStreamClass;

/**
 * A marshaller's object input stream.  Used by marshallers for compatibility with Java serialization.  Instances of
 * this class may be passed in to the overriden serialization methods for a class implementing {@link java.io.Serializable}.
 * <p>
 * This class is not part of the marshalling API; rather it is intended for marshaller implementors to make it easier
 * to develop Java serialization-compatible marshallers.
 */
public abstract class MarshallerObjectInputStream extends ObjectInputStream {
    private final Unmarshaller input;

    /**
     * Construct a new instance which delegates to the given unmarshaller.
     *
     * @param input the delegate unmarshaller
     * @throws IOException if an I/O error occurs
     * @throws SecurityException if the caller does not have permission to construct an instance of this class
     */
    protected MarshallerObjectInputStream(final Unmarshaller input) throws IOException, SecurityException {
        this.input = input;
    }

    // Delegated methods

    /** {@inheritDoc} */
    protected Object readObjectOverride() throws IOException, ClassNotFoundException {
        return input.readObject();
    }

    /** {@inheritDoc} */
    public Object readUnshared() throws IOException, ClassNotFoundException {
        return input.readObjectUnshared();
    }

    /** {@inheritDoc} */
    public int read() throws IOException {
        return input.read();
    }

    /** {@inheritDoc} */
    public int read(final byte[] buf) throws IOException {
        return input.read(buf, 0, buf.length);
    }

    /** {@inheritDoc} */
    public int read(final byte[] buf, final int off, final int len) throws IOException {
        return input.read(buf, off, len);
    }

    /** {@inheritDoc} */
    public int available() throws IOException {
        return input.available();
    }

    /** {@inheritDoc} */
    public boolean readBoolean() throws IOException {
        return input.readBoolean();
    }

    /** {@inheritDoc} */
    public byte readByte() throws IOException {
        return input.readByte();
    }

    /** {@inheritDoc} */
    public int readUnsignedByte() throws IOException {
        return input.readUnsignedByte();
    }

    /** {@inheritDoc} */
    public char readChar() throws IOException {
        return input.readChar();
    }

    /** {@inheritDoc} */
    public short readShort() throws IOException {
        return input.readShort();
    }

    /** {@inheritDoc} */
    public int readUnsignedShort() throws IOException {
        return input.readUnsignedShort();
    }

    /** {@inheritDoc} */
    public int readInt() throws IOException {
        return input.readInt();
    }

    /** {@inheritDoc} */
    public long readLong() throws IOException {
        return input.readLong();
    }

    /** {@inheritDoc} */
    public float readFloat() throws IOException {
        return input.readFloat();
    }

    /** {@inheritDoc} */
    public double readDouble() throws IOException {
        return input.readDouble();
    }

    /** {@inheritDoc} */
    public void readFully(final byte[] buf) throws IOException {
        input.readFully(buf);
    }

    /** {@inheritDoc} */
    public void readFully(final byte[] buf, final int off, final int len) throws IOException {
        input.readFully(buf, off, len);
    }

    /** {@inheritDoc} */
    public int skipBytes(final int len) throws IOException {
        return input.skipBytes(len);
    }

    /** {@inheritDoc} */
    @Deprecated
    public String readLine() throws IOException {
        return input.readLine();
    }

    /** {@inheritDoc} */
    public String readUTF() throws IOException {
        return input.readUTF();
    }

    /** {@inheritDoc} */
    public long skip(final long n) throws IOException {
        return input.skip(n);
    }

    // Unsupported methods

    /** {@inheritDoc} */
    public final void mark(final int readlimit) {
    }

    /** {@inheritDoc} */
    public final void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }

    /** {@inheritDoc} */
    public final boolean markSupported() {
        return false;
    }

    /** {@inheritDoc} */
    public final void close() throws IllegalStateException {
        throw new IllegalStateException("Stream may not be closed in this context");
    }

    /** {@inheritDoc} */
    protected final Class<?> resolveClass(final ObjectStreamClass desc) throws IllegalStateException {
        throw new IllegalStateException("Class may not be resolved in this context");
    }

    /** {@inheritDoc} */
    protected final Class<?> resolveProxyClass(final String[] interfaces) throws IllegalStateException {
        throw new IllegalStateException("Class may not be resolved in this context");
    }

    /** {@inheritDoc} */
    protected final Object resolveObject(final Object obj) throws IllegalStateException {
        throw new IllegalStateException("Object may not be resolved in this context");
    }

    /** {@inheritDoc} */
    protected final boolean enableResolveObject(final boolean enable) throws IllegalStateException {
        throw new IllegalStateException("Object resolution may not be enabled in this context");
    }

    /** {@inheritDoc} */
    protected final void readStreamHeader() throws IllegalStateException {
        throw new IllegalStateException("Stream header may not be read in this context");
    }

    /** {@inheritDoc} */
    protected final ObjectStreamClass readClassDescriptor() throws IllegalStateException {
        throw new IllegalStateException("Class descriptor may not be read in this context");
    }

    // User-implementation methods

    /** {@inheritDoc} */
    public abstract void defaultReadObject() throws IOException, ClassNotFoundException;

    /** {@inheritDoc} */
    public abstract GetField readFields() throws IOException, ClassNotFoundException;

    /** {@inheritDoc} */
    public abstract void registerValidation(final ObjectInputValidation obj, final int prio) throws NotActiveException, InvalidObjectException;
}
