private Chunk readNonblocking() throws IOException
  {
    boolean done = false;

    while (!done)
    {
      switch (state)
      {
      case SIZE_NEXT:
        if (attemptToReadSize())
        {
          if (chunkSize == 0)
          {
            return new Chunk(new byte[0]);
          }
          else
          {
            state = State.CHUNK_NEXT;
          }
        }
        else
        {
          done = true;
        }

        break;
      case CHUNK_NEXT:
        if (attemptToReadChunk())
        {
          state = State.SIZE_NEXT;
          return chunk;
        }
        else
        {
          done = true;
        }

        break;
      default:
        throw new Error("unknown state detected: " + state);
      }
    }

    return null;
  }