All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.inversoft.rest.TextResponseHandler Maven / Gradle / Ivy

Go to download

The Java 8 REST Client used in our commercial REST Client libraries such as Passport Java Client and our Java 8 Chef Client called Barista

There is a newer version: 4.2.1
Show newest version
/*
 * Copyright (c) 2016, Inversoft Inc., All Rights Reserved
 */
package com.inversoft.rest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Response handler that reads the entire body and converts it to a UTF-8 String.
 *
 * @author Brian Pontarelli
 */
public class TextResponseHandler implements RESTClient.ResponseHandler {
  @Override
  public String apply(InputStream is) throws IOException {
    if (is == null) {
      return null;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int read;
    while ((read = is.read(buf)) != -1) {
      baos.write(buf, 0, read);
    }

    return baos.toString("UTF-8");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy