Java ByteArrayOutputStream Write load(InputStream inputStream, Class type)

Here you can find the source of load(InputStream inputStream, Class type)

Description

load

License

Apache License

Declaration

public static <T> T load(InputStream inputStream, Class<T> type) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.*;

public class Main {
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    public static <T> T load(InputStream inputStream, Class<T> type) throws IOException {
        String json = new String(readStream(inputStream));
        return GSON.fromJson(json, type);
    }/*from   w w w . j  a v  a  2 s . c o  m*/

    public static byte[] readStream(InputStream inputStream) throws IOException {
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        final int bufferSize = 16384;
        try {
            final BufferedInputStream bIn = new BufferedInputStream(inputStream);
            int length;
            byte[] buffer = new byte[bufferSize];
            byte[] bufferCopy;
            while ((length = bIn.read(buffer, 0, bufferSize)) != -1) {
                bufferCopy = new byte[length];
                System.arraycopy(buffer, 0, bufferCopy, 0, length);
                output.write(bufferCopy);
            }
            bIn.close();
        } finally {
            output.close();
        }
        return output.toByteArray();
    }
}

Related

  1. load(final File file, final String encoding)
  2. load(final String resource, final byte[] defaultResult)
  3. load(InputStream in, byte[] buffer, int offset, int initialBufferSize)
  4. load(InputStream is)
  5. load(InputStream is, boolean close)
  6. load(String fileName)
  7. loadAFileToString(File f)