Example usage for java.io ByteArrayOutputStream reset

List of usage examples for java.io ByteArrayOutputStream reset

Introduction

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

Prototype

public synchronized void reset() 

Source Link

Document

Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(75);/* w ww  .j av a2 s  . c  o m*/

    String str = baos.toString();
    System.out.println("Before Resetting : " + str);

    baos.reset();

    baos.write(65);

    str = baos.toString();
    System.out.println("After Resetting : " + str);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ByteArrayOutputStream f = new ByteArrayOutputStream(12);
    System.out.println("Please 10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }//from ww  w.j  a  v a2s  .c om
    System.out.println("Buffer as a string");
    System.out.println(f.toString());
    System.out.println("Into array");
    byte b[] = f.toByteArray();
    for (int i = 0; i < b.length; i++) {
        System.out.print((char) b[i]);
    }
    System.out.println();
    OutputStream f2 = new FileOutputStream("test.txt");
    f.writeTo(f2);
    f.reset();
    System.out.println("10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }
    System.out.println("Done..");
}

From source file:Main.java

public static void reset(ByteArrayOutputStream out) {
    if (null != out)
        out.reset();
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len, int srcLen) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from   ww w. j ava  2 s .  co  m*/
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream o = new ByteArrayOutputStream(srcLen);
    try {
        o.reset();
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            o.close();
            decompresser.end();
        } catch (Exception e) {
        }
    }

    return output;
}

From source file:Main.java

public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    image.compress(Bitmap.CompressFormat.JPEG, 20, os);

    while (os.toByteArray().length / 1024 > 1024) {
        os.reset();
        image.compress(Bitmap.CompressFormat.JPEG, 20, os);
    }/*  w  w w . ja v a2s . co  m*/

    ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray());
    BitmapFactory.decodeStream(bi);
    FileOutputStream fi = new FileOutputStream(outPath);
    fi.write(os.toByteArray());
    fi.flush();
    fi.close();

}

From source file:jp.terasoluna.fw.util.ExceptionUtil.java

/**
 * ?????//from ww  w . ja  v  a 2  s.c  o m
 * 
 * <p>
 *  ??????????????
 *  ????????
 *  ???getRootCause()????????ServletException??
 * </p>
 *
 * @param throwable 
 * @return ???
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (throwable != null) {
        baos.reset();
        throwable.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());

        //throwable?Class??
        Class<? extends Throwable> throwableClass = throwable.getClass();

        // ServletException ?? getRootCause ?
        if (SERVLET_EXCEPTION_NAME.equals(throwableClass.getName())) {
            try {
                //throwable = ((ServletException) throwable).getRootCause()
                //Class??????
                Method method = throwableClass.getMethod(GET_ROOT_CAUSE);
                throwable = (Throwable) method.invoke(throwable);
            } catch (NoSuchMethodException e) {
                //???????
                log.error(e.getMessage());
                throwable = null;
            } catch (IllegalAccessException e) {
                //?????????
                log.error(e.getMessage());
                throwable = null;
            } catch (InvocationTargetException e) {
                //?????
                log.error(e.getMessage());
                throwable = null;
            }

        } else {
            throwable = throwable.getCause();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static byte[] compressBmpToBytes(Bitmap bitmap, int maxSize) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.toByteArray().length / 1024 >= maxSize) {
        baos.reset();
        options -= 5;/*from  w w  w  . j av  a2  s  .  com*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }

    return baos.toByteArray();
}

From source file:Main.java

public static Bitmap compressBmpFromBmp(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    while (baos.toByteArray().length / 1024 > 100) {
        baos.reset();
        options -= 10;/*from ww  w .j a  v a2 s  .  co  m*/
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}

From source file:Main.java

public static Bitmap compressByLimit(Bitmap bitmap, int limitSize) {
    if (bitmap == null) {
        return null;
    }/*from  w ww  . j a va  2 s.c  o  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    while (baos.size() > limitSize && options > 0) {
        options--;
        baos.reset();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
    }
    byte[] compressedData = baos.toByteArray();
    return BitmapFactory.decodeByteArray(compressedData, 0, compressedData.length);
}

From source file:Main.java

public static File getFile(Context context, Bitmap sourceImg) {
    try {//from w w  w .  j a v a2  s  . c om
        File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg");
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int options = 100;
        sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        while (bos.toByteArray().length / 1024 > 100) {
            bos.reset();
            options -= 10;
            sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        byte[] bitmapdata = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return f;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}