RandomAccessFile very slow

Brian Burton brian at burton-computer.com
Wed Jul 15 13:41:45 PDT 1998


RandomAccessFile.java wrote arrays of bytes one byte at a time.
This was extremely slow.  The following changes improved performance
20x on my system.

All the best,
++Brian


*** RandomAccessFile.java	1998/07/15 20:28:17	1.1
--- RandomAccessFile.java	1998/07/15 20:33:36
***************
*** 258,270 ****
  }
  
  public void write(byte b[]) throws IOException {
! 	write(b, 0, b.length);
  }
  
  public void write(byte b[], int off, int len) throws IOException {
! 	for (int pos=off; pos<off+len; pos++) {
! 		write((int )b[pos]);
! 	}
  }
  
  native public void write(int b) throws IOException;
--- 258,268 ----
  }
  
  public void write(byte b[]) throws IOException {
! 	writeBytes(b, 0, b.length);
  }
  
  public void write(byte b[], int off, int len) throws IOException {
! 	writeBytes(b, off, len);
  }
  
  native public void write(int b) throws IOException;
***************
*** 278,286 ****
  }
  
  final public void writeBytes(String s) throws IOException {
! 	for (int pos=0; pos<s.length(); pos++) {
! 		writeByte(s.charAt(pos) & 0xFF);
  	}
  }
  
  native private void writeBytes(byte bytes[], int off, int len);
--- 276,287 ----
  }
  
  final public void writeBytes(String s) throws IOException {
!         char[] c = s.toCharArray();
! 	byte[] b = new byte[c.length];
! 	for (int pos = 0; pos < c.length; pos++) {
! 		b[pos] = (byte)(c[pos] & 0xFF);
  	}
+ 	writeBytes(b, 0, b.length);
  }
  
  native private void writeBytes(byte bytes[], int off, int len);


More information about the kaffe mailing list