// Path: src/main/java/edu/stanford/junction/provider/jx/JXServer.java
// public static class Log {
// 	public static void d(String tag, String msg) {
// 		System.out.println(tag + ": " + msg);
// 	}
// 		
// 	public static void e(String tag, String msg) {
// 		System.err.println(tag + ": " + msg);
// 	}
// 		
// 	public static void e(String tag, String msg, Exception e) {
// 		System.err.println(tag + ": " + msg);
// 		e.printStackTrace(System.err);
// 	}
// }


import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import edu.stanford.junction.provider.jx.JXServer.Log;

package edu.stanford.junction.provider.jx.json;




/**
 * Helps read and write json messages over a WebSocket.
 *
 */
public class JsonWebSocketHandler extends JsonHandler {
	private static final int BUFFER_SIZE = 1024;
	
	private final OutputStream out;
	private final InputStream in;
	
	public JsonWebSocketHandler(InputStream in, OutputStream out) {
		this.in = in;
		this.out = new BufferedOutputStream(out);
	}

	public void sendJson(JSONObject message) throws IOException {
    	byte[] bytes = message.toString().getBytes();
    	out.write(0x00);
		out.write(bytes, 0, bytes.length);
		out.write(0x000000FF);
		out.flush();
    }

	private byte[] buffer = new byte[BUFFER_SIZE];
	private int byteCount;
	
	/**
	 * Reads a JSON object from the handler's  inputStream.
	 * This method is not thread safe.
	 */
    public JSONObject jsonFromStream() throws IOException {
		while (true) {
            try {
            	byteCount = in.read(buffer);
            	if (byteCount == -1) {
            		break;
            	}
            	
            	if (buffer[0] != 0x00) {

// Path: src/main/java/edu/stanford/junction/provider/jx/JXServer.java
// public static class Log {
// 	public static void d(String tag, String msg) {
// 		System.out.println(tag + ": " + msg);
// 	}
// 		
// 	public static void e(String tag, String msg) {
// 		System.err.println(tag + ": " + msg);
// 	}
// 		
// 	public static void e(String tag, String msg, Exception e) {
// 		System.err.println(tag + ": " + msg);
// 		e.printStackTrace(System.err);
// 	}
// }


// Path: src/main/java/edu/stanford/junction/provider/jx/json/JsonWebSocketHandler.java
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import edu.stanford.junction.provider.jx.JXServer.Log;

package edu.stanford.junction.provider.jx.json;




/**
 * Helps read and write json messages over a WebSocket.
 *
 */
public class JsonWebSocketHandler extends JsonHandler {
	private static final int BUFFER_SIZE = 1024;
	
	private final OutputStream out;
	private final InputStream in;
	
	public JsonWebSocketHandler(InputStream in, OutputStream out) {
		this.in = in;
		this.out = new BufferedOutputStream(out);
	}

	public void sendJson(JSONObject message) throws IOException {
    	byte[] bytes = message.toString().getBytes();
    	out.write(0x00);
		out.write(bytes, 0, bytes.length);
		out.write(0x000000FF);
		out.flush();
    }

	private byte[] buffer = new byte[BUFFER_SIZE];
	private int byteCount;
	
	/**
	 * Reads a JSON object from the handler's  inputStream.
	 * This method is not thread safe.
	 */
    public JSONObject jsonFromStream() throws IOException {
		while (true) {
            try {
            	byteCount = in.read(buffer);
            	if (byteCount == -1) {
            		break;
            	}
            	
            	if (buffer[0] != 0x00) {

            		Log.e(TAG, "Bad frame header found in WebSocket connection");
