tags:

views:

144

answers:

4

hello guys,

I have a 16mb binary file and I want to read bytes without using any loop. like if i want to read 16 byte i can pass 16 to some method(if there is any) and it give me my desired result... right now i am using loop for reading bytes but my application is so huge i am afraid that it do not get slow.kindly give me some hint.

thanks alot.

+1  A: 

I can give you two answers, but they're not practical. In real life you'll use a loop to read the bytes.

Valid answer 1

public byte[] readWithNoLoop(InputStream in, int size) {
   byte[] result = new byte[16777216];  // 16 MByte
   byte b = 0;

   if ((b = in.read()) >= 0) result[0] = b;
   if ((b = in.read()) >= 0) result[1] = b;
   if ((b = in.read()) >= 0) result[2] = b;

   // ...

   if ((b = in.read()) >= 0) result[16777215] = b;

   return b;
}

Valid answer 2

use a massive parallel systems that can read the file in parallel. You need 16777216 processing units and a supporting file storage system but you can read in O(1) (theoretically).


If you encounter massive perfomance problems while reading the file, check if you use a BufferedInputStream, reading bytes from a 'normal' stream kills performance. (Example)

If it still doesn't help, then have a look at the java.nio classes. They can map files to memory. The Grep example should give you a direction.

Andreas_D
+1  A: 

You can use BufferedInputStream#read(byte[] b, int off, int len)

hhbarriuso
Yes, but: 1) The implementation of that contains loops; 2) You need to loop in your client code to read from it safely. I'm not saying it's a bad idea, I'm pointing out that the requirements are nonsensical and a better answer would be "WTF do you want to do that for" rather than trying to conform to them.
Andrzej Doyle
Yes, I thought requirements were about reading n bytes in a single call instead of inefficiently reading them one by one.
hhbarriuso
A: 
import java.io.*;

public class NoLoopReader {
  public static void main (String[] args) throws Exception {
    String fileName = args[0];
    InputStream is = new BufferedInputStream(new FileInputStream(fileName));
    File file = new File(fileName);
    int size = (int)file.length();

    byte[] buffer = new byte[size];
    is.read(buffer,0,size);
  }
}
leonm
That `is.read(byte[], int, int)` contains a for loop in the implementation.
Noel M
A: 

You might check file size on disk, create appropriate buffer and use this read method to do bulk load (as you can see it calls native readBytes). Of course I have no idea if there is no loop inside of readBytes (most likely there is, but that may depend on implementation of JVM)... :)

In FileInputStream

/**
 * Reads up to <code>b.length</code> bytes of data from this input
 * stream into an array of bytes. This method blocks until some input
 * is available.
 *
 * @param      b   the buffer into which the data is read.
 * @return     the total number of bytes read into the buffer, or
 *             <code>-1</code> if there is no more data because the end of
 *             the file has been reached.
 * @exception  IOException  if an I/O error occurs.
 */
public int read(byte b[]) throws IOException {

return readBytes(b, 0, b.length); }

/**
 * Reads up to <code>len</code> bytes of data from this input stream
 * into an array of bytes. If <code>len</code> is not zero, the method
 * blocks until some input is available; otherwise, no
 * bytes are read and <code>0</code> is returned.
 *
 * @param      b     the buffer into which the data is read.
 * @param      off   the start offset in the destination array <code>b</code>
 * @param      len   the maximum number of bytes read.
 * @return     the total number of bytes read into the buffer, or
 *             <code>-1</code> if there is no more data because the end of
 *             the file has been reached.
 * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
 * <code>len</code> is negative, or <code>len</code> is greater than 
 * <code>b.length - off</code>
 * @exception  IOException  if an I/O error occurs.
 */
public int read(byte b[], int off, int len) throws IOException {

return readBytes(b, off, len); }

rihards