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

com.hannesdorfmann.httpkit.parser.LoganSquareParserWriter Maven / Gradle / Ivy

package com.hannesdorfmann.httpkit.parser;

import com.bluelinelabs.logansquare.LoganSquare;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * Uses Logan Square parser writer
 * @author Hannes Dorfmann
 */
public class LoganSquareParserWriter implements ParserWriter {

  @Override
  public Object parse(InputStream in, Class targetClass, int collectionType, String mimeType,
      String charset) throws Exception {

    if (collectionType == ParserWriter.COLLECTION_TYPE_LIST) {
      return LoganSquare.parseList(in, targetClass);
    }

    return LoganSquare.parse(in, targetClass);
  }

  @Override public void write(OutputStream out, Object value, String mimeType, String charset)
      throws Exception {

    throw new UnsupportedOperationException(
        "This write() operation is unsupported. Use writeRaw();");
  }

  @Override public void writeRaw(OutputStream out, byte[] value, String mimeType, String charset)
      throws IOException {

    ByteArrayOutputStream stringConverter = new ByteArrayOutputStream();
    stringConverter.write(value, 0, value.length);
    String str = stringConverter.toString(charset);
    PrintWriter writer = new PrintWriter(out);
    writer.print(str);
    writer.flush();
  }
}