create Zero Bytes - Android File Input Output

Android examples for File Input Output:Byte Array

Description

create Zero Bytes

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;

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

import java.util.Arrays;

public class Main {
    public static byte[] createZeroBytes(int length) {
        if (length <= 0)
            throw new IllegalArgumentException("length must be gt 0");

        byte[] bytes = null;

        try {/*from w w  w.jav a2s.  c  o  m*/
            bytes = new byte[length];
        } catch (OutOfMemoryError oom) {
            return null;
        }

        Arrays.fill(bytes, (byte) 0);
        return bytes;
    }

    public static String toString(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = null;

        try {
            buffer = new byte[1024];
        } catch (OutOfMemoryError oom) {
            baos.close();
            return null;
        }

        int totalRead = 0;
        int read = 0;

        try {
            while ((read = is.read(buffer, totalRead, 1024)) > 0) {
                baos.write(buffer, totalRead, read);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baos.close();
            return null;
        }

        String str = baos.toString();

        try {
            baos.close();
        } catch (IOException e) {
        }

        return str;
    }
}

Related Tutorials