public Response parse()

in folsom-elasticache/src/main/java/com/spotify/folsom/elasticache/ResponseParser.java [32:67]


  public Response parse(final InputStream in) throws IOException {
    try (final BufferedReader reader = new BufferedReader(new InputStreamReader(in, US_ASCII))) {
      final String response = reader.readLine().trim();

      if (response.startsWith("CONFIG cluster ")) {
        final int configVersion = Integer.valueOf(reader.readLine()); // configuration version

        final List<String> hosts = Splitter.on(' ').splitToList(reader.readLine());

        final List<HostAndPort> result = new ArrayList<>();
        for (final String host : hosts) {
          final List<String> tokens = Splitter.on('|').splitToList(host);
          if (tokens.size() != 3) {
            throw new IOException("Expected 3 parts for a host, but got " + host);
          }

          // the private IP is not guaranteed to be included, so use the CNAME
          result.add(HostAndPort.fromParts(tokens.get(0), Integer.valueOf(tokens.get(2))));
        }

        // validate complete response
        final String emptyLine = reader.readLine();
        if (!"".equals(emptyLine)) {
          throw new IOException("Expected empty trailing line, invalid server response");
        }
        final String end = reader.readLine();
        if (!"END".equals(end)) {
          throw new IOException("Expected end of response, invalid server response");
        }

        return new Response(configVersion, result);
      } else {
        throw new IOException("Unexpected server response: " + response);
      }
    }
  }