lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukas Zapletal <lukas.zaple...@qcm.cz>
Subject Re: Uneffective writeBytes and readBytes [FIX]
Date Tue, 06 Sep 2005 11:15:02 GMT
And here comes the fixes:

OutputStream:

	/**
	 * Writes an array of bytes.
	 * 
	 * @param b
	 *            the bytes to write
	 * @param length
	 *            the number of bytes to write
	 * @see InputStream#readBytes(byte[],int,int)
	 */
	public final void writeBytes(byte[] b, int length) 
		throws IOException { 

//		for (int i = 0; i < length; i++) writeByte(b[i]);

		if (bufferPosition > 0) // flush buffer
			flush();

		if (length < BUFFER_SIZE) {
				flushBuffer(b, length);
		} else {
			int pos = 0;
			int size;
			while (pos < length) {
				if (length - pos < BUFFER_SIZE) {
					size = length - pos;
				} else {
					size = BUFFER_SIZE;
				}
				System.arraycopy(b, pos, 
					buffer, 0, size); pos += size;
				flushBuffer(buffer, size);
				bufferStart += size;
			}
		}
	}

InputStream:

	public final void readBytes(byte[] b, int offset, int len)
			throws IOException {
//		if (len < BUFFER_SIZE) { // not required
//			for (int i = 0; i < len; i++)
//				// read byte-by-byte
//				b[i + offset] = (byte) readByte();
//		} else { // read all-at-once
			long start = getFilePointer();
			seekInternal(start);
			readInternal(b, offset, len);

			bufferStart = start + len;
			bufferPosition = 0; 
			bufferLength = 0;
 //		}
	}

You can try to test it in the SVN version in BufferedIndexInput and
BufferedIndexOutput, there are these methods too.

There is significant time improvement for writing and slight for
reading. I also recommend set the buffer to 8 or 16 kilobytes.

-- 
S pozdravem / Best regards
Lukas Zapletal
sefredaktor / editor-in-chief
LinuxEXPRES - opravdovy linuxovy magazin
www.LinuxEXPRES.cz
tel.:+420 777 003 843


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message