Package com.surftools.BeanstalkClient

Examples of com.surftools.BeanstalkClient.BeanstalkException


  ProtocolHandler(String host, int port) {
    try {
      socket = new Socket(host, port);
    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
  }
View Full Code Here


      is = socket.getInputStream();
      String line = new String(readInputStream(is, 0));

      String[] tokens = line.split(" ");
      if (tokens == null || tokens.length == 0) {
        throw new BeanstalkException("no response");
      }

      response = new Response();
      response.setResponseLine(line);
      String status = tokens[0];
      response.setStatus(status);
      if (tokens.length > 1) {
        response.setReponse(tokens[1]);
      }
      setState(request, response, status);

      switch (request.getExpectedResponse()) {
      case Map:
        if (response.isMatchError()) {
          break;
        }
        response.setData(parseForMap(is));
        break;
      case List:
        response.setData(parseForList(is));
        break;
      case ByteArray:
        if (response.isMatchError()) {
          break;
        }
        int length = 0;
        if (request.getExpectedDataLengthIndex() > 0 && tokens.length > request.getExpectedDataLengthIndex()) {
          try {
            length = Integer.parseInt(tokens[request.getExpectedDataLengthIndex()]);
          } catch (NumberFormatException ex) {
            length = 0;
          }
        }
        byte[] data = readInputStream(is, length);
        response.setData(data);
        break;
      default:
        break;
      }

    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
    return response;
  }
View Full Code Here

      int off = 0;
      int toRead = length - off;
      while (toRead > 0) {
        int readLength = is.read(data, off, toRead);
        if (readLength == -1) {
          throw new BeanstalkException(String.format(
              "The end of InputStream is reached - %d bytes expected, %d bytes read", length, off
                  + readLength));
        }
        off += readLength;
        toRead = length - off;
      }
      byte br = (byte) is.read();
      byte bn = (byte) is.read();
      if (br != '\r' || bn != '\n') {
        throw new BeanstalkException("The end of InputStream is reached - End of line expected, but not found");
      }
      return data;

    } catch (IOException ex) {
      throw new BeanstalkException(ex.getMessage());
    }
  }
View Full Code Here

        /**
         * prevent OutOfMemory exceptions, per leopoldkot
         */
        if (intB == -1) {
          throw new BeanstalkException("The end of InputStream is reached");
        }

        if (b == '\n' && lastByteWasReturnByte) {
          break;
        }
        if (b == '\r') {
          lastByteWasReturnByte = true;
        } else {
          if (lastByteWasReturnByte) {
            lastByteWasReturnByte = false;
            baos.write('\r');
          }
          baos.write(b);
        }
      }
      return baos.toByteArray();
    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
  }
View Full Code Here

    }
  }

  private void validateRequest(Request request) {
    if (request == null) {
      throw new BeanstalkException("null request");
    }

    String command = request.getCommand();
    if (command == null || command.length() == 0) {
      throw new BeanstalkException("null or empty command");
    }

    String[] validStates = request.getValidStates();
    if (validStates == null || validStates.length == 0) {
      throw new BeanstalkException("null or empty validStates");
    }
  }
View Full Code Here

        }
      }
    }

    if (!response.isMatchOk() && !response.isMatchError()) {
      throw new BeanstalkException(status);
    }
  }
View Full Code Here

          continue;
        }
        map.put(values[0], values[1]);
      }
    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
    return map;
  }
View Full Code Here

          continue;
        }
        list.add(line.substring(2));
      }
    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
    return list;
  }
View Full Code Here

  public void close() {
    if (socket != null && !socket.isClosed()) {
      try {
        socket.close();
      } catch (Exception e) {
        throw new BeanstalkException(e.getMessage());
      }
    }
  }
View Full Code Here

      oos.flush();
      bytes = baos.toByteArray();
      oos.close();
      baos.close();
    } catch (Exception e) {
      throw new BeanstalkException(e.getMessage());
    }
    return bytes;
  }
View Full Code Here

TOP

Related Classes of com.surftools.BeanstalkClient.BeanstalkException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.