public byte[] read(InputStream is) throws IOException
	{
		byte[] readBuf = new byte[bufSize];
		int totalRead = 0;
		int hdrEndIdx = 0;
		byte[] leftoverBuf = null;
		byte[] headerBuf = new byte[bufSize];
		int headerBufRead = 0;
		int read;

        System.out.println("");
        Log.i(LOG_TAG, "read started");

		while ((read = is.read(readBuf)) > 0)
		{
			totalRead += read;

			hdrEndIdx = getHeaderEndIdx(readBuf);

			//increase header buffer size if needed
			if (totalRead > headerBuf.length)
			{
				byte[] temp = new byte[headerBuf.length * 2];
				System.arraycopy(headerBuf, 0, temp, 0, headerBuf.length);
				headerBuf = temp;
			}

			int headerBytesLength = hdrEndIdx == 0 ? readBuf.length : hdrEndIdx;
			System.arraycopy(readBuf, 0, headerBuf, headerBufRead, headerBytesLength);
			headerBufRead += headerBytesLength;

			if (hdrEndIdx != 0)
			{
				if (hdrEndIdx != totalRead)
				{
					leftoverBuf = new byte[totalRead - hdrEndIdx];
					System.arraycopy(readBuf, hdrEndIdx, leftoverBuf, 0, leftoverBuf.length);
				}

				break;
			}
		}

		if (totalRead > 0)
		{
			decodeHeader(headerBuf);
		}

        Log.i(LOG_TAG, "read ended");

		return leftoverBuf;
	}