spectator-ext-ipc/src/main/java/com/netflix/spectator/ipc/http/HttpUtils.java [33:114]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
final class HttpUtils {
  private HttpUtils() {
  }

  /** Empty byte array constant. */
  static final byte[] EMPTY = new byte[0];

  private static final String DEFAULT = "default";

  private static final Pattern PREFIX = Pattern.compile("^([^.-]+).*$");

  /**
   * Extract a client name based on the host. This will currently select up
   * to the first dash or dot in the name. The goal is to have a reasonable
   * name, but avoid a large explosion in number of names in dynamic environments
   * such as EC2. Examples:
   *
   * <pre>
   * name      host
   * ----------------------------------------------------
   * foo       foo.test.netflix.com
   * ec2       ec2-127-0-0-1.compute-1.amazonaws.com
   * </pre>
   */
  static String clientNameForHost(String host) {
    Matcher m = PREFIX.matcher(host);
    return m.matches() ? m.group(1) : DEFAULT;
  }

  /**
   * Extract a client name based on the uri host. See {@link #clientNameForHost(String)}
   * for more details.
   */
  static String clientNameForURI(URI uri) {
    String host = uri.getHost();
    return (host == null) ? DEFAULT : clientNameForHost(host);
  }

  /** Wrap GZIPOutputStream allowing the user to override the compression level. */
  static class GzipLevelOutputStream extends GZIPOutputStream {
    /** Creates a new output stream with a default compression level. */
    GzipLevelOutputStream(OutputStream outputStream) throws IOException {
      super(outputStream);
    }

    /** Set the compression level for the underlying deflater. */
    void setLevel(int level) {
      def.setLevel(level);
    }
  }

  /**
   * Compress a byte array using GZIP's default compression level.
   */
  static byte[] gzip(byte[] data) throws IOException {
    return gzip(data, Deflater.DEFAULT_COMPRESSION);
  }

  /**
   * Compress a byte array using GZIP with the given compression level.
   */
  static byte[] gzip(byte[] data, int level) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
    try (GzipLevelOutputStream out = new GzipLevelOutputStream(baos)) {
      out.setLevel(level);
      out.write(data);
    }
    return baos.toByteArray();
  }

  /** Decompress a GZIP compressed byte array. */
  @SuppressWarnings("PMD.AssignmentInOperand")
  static byte[] gunzip(byte[] data) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length * 10);
    try (InputStream in = new GZIPInputStream(new ByteArrayInputStream(data))) {
      byte[] buffer = new byte[4096];
      int length;
      while ((length = in.read(buffer)) > 0) {
        baos.write(buffer, 0, length);
      }
    }
    return baos.toByteArray();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



spectator-ext-sandbox/src/main/java/com/netflix/spectator/sandbox/HttpUtils.java [33:114]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
final class HttpUtils {
  private HttpUtils() {
  }

  /** Empty byte array constant. */
  static final byte[] EMPTY = new byte[0];

  private static final String DEFAULT = "default";

  private static final Pattern PREFIX = Pattern.compile("^([^.-]+).*$");

  /**
   * Extract a client name based on the host. This will currently select up
   * to the first dash or dot in the name. The goal is to have a reasonable
   * name, but avoid a large explosion in number of names in dynamic environments
   * such as EC2. Examples:
   *
   * <pre>
   * name      host
   * ----------------------------------------------------
   * foo       foo.test.netflix.com
   * ec2       ec2-127-0-0-1.compute-1.amazonaws.com
   * </pre>
   */
  static String clientNameForHost(String host) {
    Matcher m = PREFIX.matcher(host);
    return m.matches() ? m.group(1) : DEFAULT;
  }

  /**
   * Extract a client name based on the uri host. See {@link #clientNameForHost(String)}
   * for more details.
   */
  static String clientNameForURI(URI uri) {
    String host = uri.getHost();
    return (host == null) ? DEFAULT : clientNameForHost(host);
  }

  /** Wrap GZIPOutputStream allowing the user to override the compression level. */
  static class GzipLevelOutputStream extends GZIPOutputStream {
    /** Creates a new output stream with a default compression level. */
    GzipLevelOutputStream(OutputStream outputStream) throws IOException {
      super(outputStream);
    }

    /** Set the compression level for the underlying deflater. */
    void setLevel(int level) {
      def.setLevel(level);
    }
  }

  /**
   * Compress a byte array using GZIP's default compression level.
   */
  static byte[] gzip(byte[] data) throws IOException {
    return gzip(data, Deflater.DEFAULT_COMPRESSION);
  }

  /**
   * Compress a byte array using GZIP with the given compression level.
   */
  static byte[] gzip(byte[] data, int level) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
    try (GzipLevelOutputStream out = new GzipLevelOutputStream(baos)) {
      out.setLevel(level);
      out.write(data);
    }
    return baos.toByteArray();
  }

  /** Decompress a GZIP compressed byte array. */
  @SuppressWarnings("PMD.AssignmentInOperand")
  static byte[] gunzip(byte[] data) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length * 10);
    try (InputStream in = new GZIPInputStream(new ByteArrayInputStream(data))) {
      byte[] buffer = new byte[4096];
      int length;
      while ((length = in.read(buffer)) > 0) {
        baos.write(buffer, 0, length);
      }
    }
    return baos.toByteArray();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



