package be.jeffcheasey88.peeratcode.framework;

import java.io.IOException;

public class WebSocketReader extends HttpReader{

	public WebSocketReader(HttpReader origin) throws Exception{
		super(origin);
	}

	
	
	@Override
	public String readLine() throws IOException{
		//read websocket found on StackOverFlow
		int buffLenth = 1024;
		int len = 0;
		byte[] b = new byte[buffLenth];
		// rawIn is a Socket.getInputStream();
		while (true){
			len = read(b);
			if (len != -1){
				byte rLength = 0;
				int rMaskIndex = 2;
				int rDataStart = 0;
				// b[0] is always text in my case so no need to check;
				byte data = b[1];
				byte op = (byte) 127;
				rLength = (byte) (data & op);

				if (rLength == (byte) 126)
					rMaskIndex = 4;
				if (rLength == (byte) 127)
					rMaskIndex = 10;

				byte[] masks = new byte[4];

				int j = 0;
				int i = 0;
				for (i = rMaskIndex; i < (rMaskIndex + 4); i++){
					masks[j] = b[i];
					j++;
				}

				rDataStart = rMaskIndex + 4;

				int messLen = len - rDataStart;

				byte[] message = new byte[messLen];

				for (i = rDataStart, j = 0; i < len; i++, j++){
					message[j] = (byte) (b[i] ^ masks[j % 4]);
				}

				return new String(message);

			} else
				break;
		}
		return null;
	}
}
