/*
 * Copyright 2014, Kaazing Corporation. All rights reserved.
 *
 * 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
 *
 * 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 org.kaazing.netx.ws.internal.io;

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

public final class WsInputStreamImpl extends InputStream {

    private final InputStream in;
    private final byte[] header;

    private int headerOffset;
    private int payloadOffset;
    private int payloadLength;

    public WsInputStreamImpl(InputStream in) {
        this.in = in;
        this.header = new byte[10];
        this.payloadOffset = -1;
    }

    @Override
    public int available() throws IOException {
        // TODO:
        return in.available();
    }

    @Override
    public int read() throws IOException {

        while (payloadLength == 0) {
            while (payloadOffset == -1) {
                byte headerByte = (byte) in.read();
                if (headerByte == -1) {
                    return -1;
                }
                header[headerOffset++] = headerByte;
                switch (headerOffset) {
                case 1:
                    int opcode = header[0] & 0x07;
                    switch (opcode) {
                    case 0x00:
                    case 0x02:
                        break;
                    default:
                        // TODO: skip
                        throw new IOException("Non-binary frame");
                    }
                    break;
                case 2:
                    boolean masked = (header[1] & 0x80) != 0x00;
                    if (masked) {
                        throw new IOException("Masked server-to-client frame");
                    }
                    switch (header[1] & 0x7f) {
                    case 126:
                    case 127:
                        break;
                    default:
                        payloadOffset = 0;
                        payloadLength = payloadLength(header);
                    }
                    break;
                case 6:
                    switch (header[1] & 0x7f) {
                    case 126:
                        payloadOffset = 0;
                        payloadLength = payloadLength(header);
                    default:
                        break;
                    }
                    break;
                case 10:
                    switch (header[1] & 0x7f) {
                    case 127:
                        payloadOffset = 0;
                        payloadLength = payloadLength(header);
                    default:
                        break;
                    }
                    break;
                }
            }
        }

        int b = in.read();

        if (payloadOffset++ == payloadLength) {
            headerOffset = 0;
            payloadOffset = -1;
            payloadLength = 0;
        }

        return b;
    }

    @Override
    public int read(byte[] b) throws IOException {
        return super.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return super.read(b, off, len);
    }

    @Override
    public long skip(long n) throws IOException {
        return super.skip(n);
    }

    @Override
    public void close() throws IOException {
        in.close();
    }

    private static int payloadLength(byte[] header) {
        int length = header[1] & 0x7f;
        switch (length) {
        case 126:
            return (header[2] & 0xff) << 8 | (header[3] & 0xff);
        case 127:
            return (header[2] & 0xff) << 56 | (header[3] & 0xff) << 48 |
                   (header[4] & 0xff) << 40 | (header[4] & 0xff) << 32 |
                   (header[6] & 0xff) << 24 | (header[5] & 0xff) << 16 |
                   (header[8] & 0xff) << 8  | (header[9] & 0xff);
        default:
            return length;
        }
    }

}

@@ -2527,17 +2527,17 @@
    case 
-6
+4
 :%0A      

Fix total header bytes needed (4) requirement for 2-byte extended length