Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why initialize this byte array to 1024

Tags:

java

android

I'm relatively new to Java and I'm attempting to write a simple android app. I have a large text file with about 3500 lines in the assets folder of my applications and I need to read it into a string. I found a good example about how to do this but I have a question about why the byte array is initialized to 1024. Wouldn't I want to initialize it to the length of my text file? Also, wouldn't I want to use char, not byte? Here is the code:

private void populateArray(){
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open("3500LineTextFile.txt");
    } catch (IOException e) {
        Log.e("IOException populateArray", e.getMessage());
    }
    String s = readTextFile(inputStream);
    // Add more code here to populate array from string
}

private String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    inputStream.length
    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        Log.e("IOException readTextFile", e.getMessage());
    }
    return outputStream.toString();
}

EDIT: Based on your suggestions, I tried this approach. Is it any better? Thanks.

private void populateArray(){
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    Reader iStreamReader = null;
    try {
        inputStream = assetManager.open("List.txt");
        iStreamReader = new InputStreamReader(inputStream, "UTF-8");
    } catch (IOException e) {
        Log.e("IOException populateArray", e.getMessage());
    }
    String String = readTextFile(iStreamReader);
    // more code here
}

private String readTextFile(InputStreamReader inputStreamReader) {
    StringBuilder sb = new StringBuilder();
    char buf[] = new char[2048];
    int read;
    try {
        do {
            read = inputStreamReader.read(buf, 0, buf.length);
            if (read>0) {
                sb.append(buf, 0, read);
            }
        } while (read>=0);
    } catch (IOException e) {
        Log.e("IOException readTextFile", e.getMessage());
    }
    return sb.toString();
}
like image 918
b10hazard Avatar asked Jul 02 '11 15:07

b10hazard


People also ask

Why do we need byte array?

The crucial thing about a byte array is that it gives indexed (fast), precise, raw access to each 8-bit value being stored in that part of memory, and you can operate on those bytes to control every single bit.

What is the byte size of an array?

If we want to determine the size of array, means how many elements present in the array, we have to write the calculation with the help of sizeof() operator. Sizeof( arr[] ) / sizeof( arr[0] ) ; Here, the size of arr [] is 5 and each character takes memory 2 bytes. So, the total memory is consumed = ( 5 * 2 ) bytes.

What is meant by byte array?

The bytearray() method returns a bytearray object, which is an array of bytes. It returns a mutable series of integers between 0 and 256. The source parameter of the ByteArray is used to initialize the array.

What is the default value of byte array in C#?

The default values of numeric array elements are set to zero, and reference elements are set to null. Since byte represents integer values from 0 to 255 , all elements are set to 0 in your authToken array.


2 Answers

This example is not good at all. It's full of bad practices (hiding exceptions, not closing streams in finally blocks, not specify an explicit encoding, etc.). It uses a 1024 bytes long buffer because it doesn't have any way of knowing the length of the input stream.

Read the Java IO tutorial to learn how to read text from a file.

like image 148
JB Nizet Avatar answered Oct 14 '22 20:10

JB Nizet


You are reading the file into a buffer of 1024 Bytes. Then those 1024 bytes are written to outputStream. This process repeats until the whole file is read into the outputStream. As JB Nizet mentioned the example is full of bad practices.

like image 23
Kazuo Avatar answered Oct 14 '22 21:10

Kazuo