public long open(String uri) throws IOException {
    if (opened) {
      throw new IOException("Attempting to re-open an OPENED datasource for uri " + uri);
    }
    log.debug("[{}]:Open(): {}", session, uri);
    try {
      String host = getHost(uri);
      this.uri = uri;
      bytesRead = 0;
      bytesPos = 0;
      bytesAvailable = 0;

      circularByteBuffer = new CircularByteBuffer(PushBufferDataSource.PIPE_SIZE);
      in = circularByteBuffer.getInputStream();
      out = circularByteBuffer.getOutputStream();

      remoteServer = new Socket();
      remoteServer.connect(new java.net.InetSocketAddress(host, 7818), 2000);
      this.remoteReader = new DataInputStream(remoteServer.getInputStream());
      this.remoteWriter = remoteServer.getOutputStream();

      sendStringCommandWithReply("OPEN " + getPath(uri));
      String strSize = sendStringCommandWithReply("SIZE");
      if (strSize != null) {
        try {
          size = Long.parseLong(strSize.split(" ")[0]);
        } catch (Throwable t) {
          log.error("[{}]:Failed to get Size", session, t);
          size = -1;
        }
      }
      log.debug("SIZE got {} for {}", size, uri);
      opened = true;
      if (listener != null) {
        listener.onOpen(this);
      }
    } catch (Throwable t) {
      log.error("[{}]:Unable to open: {}", session, uri, t);
    }

    return size;
  }
 @Override
 public void flush() {
   log.debug("[{}]:Flushing()", session);
   if (circularByteBuffer != null) {
     circularByteBuffer.clear();
   }
   size = 0;
   bytesPos = 0;
   bytesRead = 0;
   bytesAvailable = 0;
 }
  public void close() {
    log.debug("[{}]:Close()", session);
    try {
      if (remoteServer != null) {
        try {
          sendStringCommandWithReply("CLOSE");
        } catch (Throwable t) {
        }
        remoteServer.close();
      }
    } catch (IOException e) {
      // e.printStackTrace();
    }
    remoteServer = null;
    remoteReader = null;
    remoteWriter = null;

    try {
      circularByteBuffer.clear();
      if (in != null) in.close();
    } catch (IOException e) {
      // e.printStackTrace();
    }
    try {
      if (out != null) out.close();
    } catch (IOException e) {
      // e.printStackTrace();
    }

    buffer = null;
    circularByteBuffer = null;
    in = null;
    out = null;

    if (listener != null) {
      listener.onClose(this);
    }
    // MiniClient.get().getHttpBridge().removeSession(session);

    opened = false;
  }
 @Override
 public int bufferAvailable() {
   if (circularByteBuffer == null) return MAX_BUFFER;
   return circularByteBuffer.getSpaceLeft();
 }