У меня есть 16-битный двоичный файл, и я хочу читать байты без использования какого-либо цикла. например, если я хочу читать 16 байт, я могу передать 16 некоторым методам (если они есть), и он дает мне мой желаемый результат... прямо сейчас я использую цикл для чтения байтов, но мое приложение настолько велико, что я боюсь, что он не получает slow.kindly дать мне некоторый намек.
Большое спасибо.
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);
}
}
is.read(byte[], int, int)
содержит цикл for в реализации.
Я могу дать вам два ответа, но они не практичны. В реальной жизни вы будете использовать цикл для чтения байтов.
Действительный ответ 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;
}
Действительный ответ 2
используйте массивные параллельные системы, которые могут читать файл параллельно. Вам нужны 16777216 процессоров и поддерживающая система хранения файлов, но вы можете читать в O (1) (теоретически).
Если вы сталкиваетесь с огромными проблемами с производительностью при чтении файла, проверьте, используете ли вы BufferedInputStream, чтение байтов из "нормального" потока убивает производительность. (Пример)
Если это все еще не помогает, посмотрите на классы java.nio
. Они могут отображать файлы в память. Пример Grep должен дать вам направление.
Вы можете использовать BufferedInputStream # read (byte [] b, int off, int len)
Вы можете проверить размер файла на диске, создать соответствующий буфер и использовать этот метод чтения для массовой загрузки (как вы можете видеть, он вызывает native readBytes). Конечно, я понятия не имею, нет ли цикла внутри readBytes (скорее всего, есть, но это может зависеть от реализации JVM)...:)
В 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); }