Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:com.eviware.soapui.support.Tools.java

public static ByteArrayOutputStream readAll(InputStream instream, long maxSize) throws IOException {
    ByteArrayOutputStream outstream = new ByteArrayOutputStream(4096);

    readAndWrite(instream, maxSize, outstream);

    outstream.close();
    return outstream;
}

From source file:Main.java

public static String readFromAsstes(Context context, String fileName) {
    AssetManager assetManager = context.getAssets();
    ByteArrayOutputStream bos = null;
    InputStream inputStream = null;
    String result = null;//from ww w  . java 2s .  c  om
    try {
        inputStream = assetManager.open(fileName, AssetManager.ACCESS_STREAMING);
        bos = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int length;
        while ((length = inputStream.read(data)) != -1) {
            bos.write(data, 0, length);
        }
        result = bos.toString().replaceAll("\\s*", "");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (inputStream != null)
                inputStream.close();
        } catch (IOException ignored) {
        }
    }
    return result;
}

From source file:Main.java

public static String objectSerializableToString(Serializable object) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream os = null;
    try {/* w w w .ja v  a 2s  .co  m*/
        if (object != null) {
            baos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(baos);
            os.writeObject(object);
            os.flush();
            baos.flush();
            return baos.toString("UTF-8");
        }
    } catch (Throwable e) {
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (Throwable e2) {
        }

        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Throwable e2) {
        }
    }
    return null;
}

From source file:de.kapsi.net.daap.DaapUtil.java

/**
 * Serializes the <code>chunk</code> and compresses it optionally.
 * The serialized data is returned as a byte-Array.
 *///  w  ww.j  a  va2  s.c om
public static final byte[] serialize(Chunk chunk, boolean compress) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(chunk.getSize());

    if (compress) {
        GZIPOutputStream gzip = new GZIPOutputStream(buffer);
        chunk.serialize(gzip);
        gzip.finish();
        gzip.close();
    } else {
        chunk.serialize(buffer);
        buffer.flush();
        buffer.close();
    }

    return buffer.toByteArray();
}

From source file:com.jlgranda.fede.ejb.url.reader.FacturaElectronicaURLReader.java

/**
 * Obtiene la lista de objetos factura para el sujeto en fede
 *
 * @param urls URLs hacia los archivo XML o ZIP a leer
 * @return una lista de instancias FacturaReader
 *//*  ww w. j  av  a  2  s .  c o  m*/
public static FacturaReader getFacturaElectronica(String url) throws Exception {
    FacturaReader facturaReader = null;

    if (url.endsWith(".xml")) {
        String xml = FacturaElectronicaURLReader.read(url);
        facturaReader = new FacturaReader(FacturaUtil.read(xml), xml, url);
    } else if (url.endsWith(".zip")) {
        URL url_ = new URL(url);
        InputStream is = url_.openStream();
        ZipInputStream zis = new ZipInputStream(is);
        try {

            ZipEntry entry = null;
            String tmp = null;
            ByteArrayOutputStream fout = null;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().toLowerCase().endsWith(".xml")) {
                    fout = new ByteArrayOutputStream();
                    for (int c = zis.read(); c != -1; c = zis.read()) {
                        fout.write(c);
                    }

                    tmp = new String(fout.toByteArray(), Charset.defaultCharset());
                    facturaReader = new FacturaReader(FacturaUtil.read(tmp), tmp, url);
                    fout.close();
                }
                zis.closeEntry();
            }
            zis.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zis);
        }
    }

    return facturaReader;
}

From source file:Main.java

public static String file2String(File file) {
    FileInputStream fis = null;/*from  w ww .  j  a  va2 s  . c  o m*/
    ByteArrayOutputStream baos = null;
    try {
        fis = new FileInputStream(file);
        baos = new ByteArrayOutputStream();
        int i;
        while ((i = fis.read()) != -1) {
            baos.write(i);
        }
        String str = baos.toString();
        return str;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.liferay.util.Http.java

public static String URLtoString(URL url) throws IOException {
    String xml = null;// ww  w .j  a  va2  s  .c  o m

    if (url != null) {
        URLConnection con = url.openConnection();

        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        con.setRequestProperty("User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

        InputStream is = con.getInputStream();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] bytes = new byte[512];

        for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) {

            buffer.write(bytes, 0, i);
        }

        xml = new String(buffer.toByteArray());

        is.close();
        buffer.close();
    }

    return xml;
}

From source file:crow.util.Util.java

/**
 * inputStream UTF-8??//  w  w w . j a  v  a2s  . co  m
 * 
 */
public static String inputStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte arr[] = new byte[32];
    int len = 0;
    try {
        while ((len = is.read(arr)) != -1) {
            out.write(arr, 0, len);
        }
    } finally {
        try {
            is.close();
            out.close();
        } catch (IOException e) {
            // do nothing
        }
    }
    return out.toString(HTTP.UTF_8);
}

From source file:com.hat.tools.HttpUtils.java

public static void SendRequest(String url, Handler handler) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();

    byte[] data = null;
    InputStream in = null;//from  w w  w. java  2s  . c  o m
    try {
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            in = response.getEntity().getContent();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            data = out.toByteArray();
            Message msg = new Message();
            msg.what = HTTP_FINISH;
            Bundle bundle = new Bundle();
            bundle.putByteArray("data", data);
            msg.setData(bundle);
            handler.sendMessage(msg);
            out.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.evolveum.midpoint.repo.sql.util.RUtil.java

public static byte[] getByteArrayFromXml(String xml, boolean compress) {
    byte[] array;

    GZIPOutputStream gzip = null;
    try {//from   www.  j a v a  2  s  .  com
        if (compress) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(xml.getBytes("utf-8"));
            gzip.close();
            out.close();

            array = out.toByteArray();
        } else {
            array = xml.getBytes("utf-8");
        }
    } catch (Exception ex) {
        throw new SystemException("Couldn't save full xml object, reason: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(gzip);
    }

    return array;
}