// Path: src/net/sourcewalker/olv/messages/DecodeException.java
// public class DecodeException extends Exception {
// 
//     private static final long serialVersionUID = 1102616192132639136L;
// 
//     public DecodeException() {
//         super();
//     }
// 
//     public DecodeException(String detailMessage, Throwable throwable) {
//         super(detailMessage, throwable);
//     }
// 
//     public DecodeException(String detailMessage) {
//         super(detailMessage);
//     }
// 
//     public DecodeException(Throwable throwable) {
//         super(throwable);
//     }
// 
// }
// 
// Path: src/net/sourcewalker/olv/messages/LiveViewEvent.java
// public abstract class LiveViewEvent extends LiveViewMessage {
// 
//     public LiveViewEvent(byte id) {
//         super(id);
//     }
// 
//     public abstract void readData(ByteBuffer buffer);
// 
// }
// 
// Path: src/net/sourcewalker/olv/messages/MessageDecoder.java
// public final class MessageDecoder {
// 
//     private static final String TAG = "MessageDecoder";
// 
//     private static LiveViewEvent newInstanceForId(byte id)
//             throws DecodeException {
//         switch (id) {
//         case MessageConstants.MSG_GETCAPS_RESP:
//             return new CapsResponse();
//         case MessageConstants.MSG_SETVIBRATE_ACK:
//         case MessageConstants.MSG_SETLED_ACK:
//             return new ResultEvent(id);
//         case MessageConstants.MSG_GETTIME:
//             return new GetTime();
//         case MessageConstants.MSG_GETMENUITEMS:
//             return new GetMenuItems();
//         case MessageConstants.MSG_DEVICESTATUS:
//             return new DeviceStatus();
//         case MessageConstants.MSG_NAVIGATION:
//             return new Navigation();
//         default:
//             throw new DecodeException("No message found matching ID: " + id);
//         }
//     }
// 
//     public static final LiveViewEvent decode(byte[] message, int length)
//             throws DecodeException {
//         if (length < 4) {
//             Log.w(TAG, "Got empty message!");
//             throw new DecodeException("Can't decode empty message!");
//         } else {
//             ByteBuffer buffer = ByteBuffer.wrap(message, 0, length);
//             byte msgId = buffer.get();
//             buffer.get();
//             int payloadLen = buffer.getInt();
//             if (payloadLen + 6 == length) {
//                 LiveViewEvent result = newInstanceForId(msgId);
//                 result.readData(buffer);
//                 return result;
//             } else {
//                 throw new DecodeException("Invalid message length: "
//                         + message.length + " (should be " + (payloadLen + 6)
//                         + ")");
//             }
//         }
//     }
// 
// }


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import net.sourcewalker.olv.messages.DecodeException;
import net.sourcewalker.olv.messages.LiveViewEvent;
import net.sourcewalker.olv.messages.MessageDecoder;
        ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
        int needRead = 1;
        ReaderState state = ReaderState.ID;
        do {
            byte read = (byte) stream.read();
            if (read == -1) {
                throw new DecodeException("Invalid message received (length="
                        + msgStream.size() + ")");
            }
            needRead--;
            msgStream.write(read);
            if (needRead == 0) {
                switch (state) {
                case ID:
                    state = ReaderState.HEADER_LEN;
                    needRead = 1;
                    break;
                case HEADER_LEN:
                    state = ReaderState.HEADER;
                    needRead = read;
                    break;
                case HEADER:
                    int payloadSize = getLastInt(msgStream);
                    state = ReaderState.PAYLOAD;
                    needRead = payloadSize;
                    break;
                }
            }
        } while (needRead > 0);
        byte[] msgArray = msgStream.toByteArray();
// Path: src/net/sourcewalker/olv/messages/DecodeException.java
// public class DecodeException extends Exception {
// 
//     private static final long serialVersionUID = 1102616192132639136L;
// 
//     public DecodeException() {
//         super();
//     }
// 
//     public DecodeException(String detailMessage, Throwable throwable) {
//         super(detailMessage, throwable);
//     }
// 
//     public DecodeException(String detailMessage) {
//         super(detailMessage);
//     }
// 
//     public DecodeException(Throwable throwable) {
//         super(throwable);
//     }
// 
// }
// 
// Path: src/net/sourcewalker/olv/messages/LiveViewEvent.java
// public abstract class LiveViewEvent extends LiveViewMessage {
// 
//     public LiveViewEvent(byte id) {
//         super(id);
//     }
// 
//     public abstract void readData(ByteBuffer buffer);
// 
// }
// 
// Path: src/net/sourcewalker/olv/messages/MessageDecoder.java
// public final class MessageDecoder {
// 
//     private static final String TAG = "MessageDecoder";
// 
//     private static LiveViewEvent newInstanceForId(byte id)
//             throws DecodeException {
//         switch (id) {
//         case MessageConstants.MSG_GETCAPS_RESP:
//             return new CapsResponse();
//         case MessageConstants.MSG_SETVIBRATE_ACK:
//         case MessageConstants.MSG_SETLED_ACK:
//             return new ResultEvent(id);
//         case MessageConstants.MSG_GETTIME:
//             return new GetTime();
//         case MessageConstants.MSG_GETMENUITEMS:
//             return new GetMenuItems();
//         case MessageConstants.MSG_DEVICESTATUS:
//             return new DeviceStatus();
//         case MessageConstants.MSG_NAVIGATION:
//             return new Navigation();
//         default:
//             throw new DecodeException("No message found matching ID: " + id);
//         }
//     }
// 
//     public static final LiveViewEvent decode(byte[] message, int length)
//             throws DecodeException {
//         if (length < 4) {
//             Log.w(TAG, "Got empty message!");
//             throw new DecodeException("Can't decode empty message!");
//         } else {
//             ByteBuffer buffer = ByteBuffer.wrap(message, 0, length);
//             byte msgId = buffer.get();
//             buffer.get();
//             int payloadLen = buffer.getInt();
//             if (payloadLen + 6 == length) {
//                 LiveViewEvent result = newInstanceForId(msgId);
//                 result.readData(buffer);
//                 return result;
//             } else {
//                 throw new DecodeException("Invalid message length: "
//                         + message.length + " (should be " + (payloadLen + 6)
//                         + ")");
//             }
//         }
//     }
// 
// }


// Path: src/net/sourcewalker/olv/service/EventReader.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import net.sourcewalker.olv.messages.DecodeException;
import net.sourcewalker.olv.messages.LiveViewEvent;
import net.sourcewalker.olv.messages.MessageDecoder;

        ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
        int needRead = 1;
        ReaderState state = ReaderState.ID;
        do {
            byte read = (byte) stream.read();
            if (read == -1) {
                throw new DecodeException("Invalid message received (length="
                        + msgStream.size() + ")");
            }
            needRead--;
            msgStream.write(read);
            if (needRead == 0) {
                switch (state) {
                case ID:
                    state = ReaderState.HEADER_LEN;
                    needRead = 1;
                    break;
                case HEADER_LEN:
                    state = ReaderState.HEADER;
                    needRead = read;
                    break;
                case HEADER:
                    int payloadSize = getLastInt(msgStream);
                    state = ReaderState.PAYLOAD;
                    needRead = payloadSize;
                    break;
                }
            }
        } while (needRead > 0);
        byte[] msgArray = msgStream.toByteArray();
        return MessageDecoder.decode(msgArray, msgArray.length);