diff src/org/tmatesoft/hg/internal/ByteArrayDataAccess.java @ 599:55b7987c1796

Do not instantiate intermediate arrays
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 03 May 2013 15:29:26 +0200
parents 6c22bdc0bdfd
children 46b56864b483
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/ByteArrayDataAccess.java	Fri May 03 15:19:18 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/ByteArrayDataAccess.java	Fri May 03 15:29:26 2013 +0200
@@ -54,9 +54,32 @@
 		if (len > (this.length - pos)) {
 			throw new IOException();
 		}
-		System.arraycopy(data, pos, buf, off, len);
+		System.arraycopy(data, offset+pos, buf, off, len);
 		pos += len;
 	}
+	@Override
+	public int readInt() throws IOException {
+		// overridden not to create an intermediate array
+		if (length - pos < 4) {
+			throw new IOException();
+		}
+		int x = offset + pos;
+		int rv = data[x++] << 24 | (data[x++] & 0xFF) << 16 | (data[x++] & 0xFF) << 8 | (data[x] & 0xFF);
+		pos += 4;
+		return rv;
+	}
+	@Override
+	public long readLong() throws IOException {
+		// overridden not to create an intermediate array
+		if (length - pos < 8) {
+			throw new IOException();
+		}
+		int x = offset + pos;
+		int i1 = data[x++] << 24 | (data[x++] & 0xFF) << 16 | (data[x++] & 0xFF) << 8 | (data[x++] & 0xFF);
+		int i2 = data[x++] << 24 | (data[x++] & 0xFF) << 16 | (data[x++] & 0xFF) << 8 | (data[x] & 0xFF);
+		pos += 8;
+		return ((long) i1) << 32 | ((long) i2 & 0x0FFFFFFFFl);
+	}
 
 	@Override
 	public ByteArrayDataAccess reset() {