diff src/org/tmatesoft/hg/internal/DataAccess.java @ 157:d5268ca7715b

Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 09 Mar 2011 05:22:17 +0100
parents src/com/tmate/hgkit/fs/DataAccess.java@9429c7bd1920 src/com/tmate/hgkit/fs/DataAccess.java@e93101b97e4a
children b413b16d10a5
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/DataAccess.java	Wed Mar 02 01:06:09 2011 +0100
+++ b/src/org/tmatesoft/hg/internal/DataAccess.java	Wed Mar 09 05:22:17 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 TMate Software Ltd
+ * Copyright (c) 2010-2011 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,6 +17,7 @@
 package org.tmatesoft.hg.internal;
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
 
 /**
  * relevant parts of DataInput, non-stream nature (seek operation), explicit check for end of data.
@@ -31,6 +32,18 @@
 	public boolean isEmpty() {
 		return true;
 	}
+	public long length() {
+		return 0;
+	}
+	/**
+	 * get this instance into initial state
+	 * @throws IOException
+	 * @return <code>this</code> for convenience
+	 */
+	public DataAccess reset() throws IOException {
+		// nop, empty instance is always in the initial state
+		return this;
+	}
 	// absolute positioning
 	public void seek(long offset) throws IOException {
 		throw new UnsupportedOperationException();
@@ -58,7 +71,32 @@
 	public void readBytes(byte[] buf, int offset, int length) throws IOException {
 		throw new UnsupportedOperationException();
 	}
+	// reads bytes into ByteBuffer, up to its limit or total data length, whichever smaller
+	// FIXME perhaps, in DataAccess paradigm (when we read known number of bytes, we shall pass specific byte count to read) 
+	public void readBytes(ByteBuffer buf) throws IOException {
+//		int toRead = Math.min(buf.remaining(), (int) length());
+//		if (buf.hasArray()) {
+//			readBytes(buf.array(), buf.arrayOffset(), toRead);
+//		} else {
+//			byte[] bb = new byte[toRead];
+//			readBytes(bb, 0, bb.length);
+//			buf.put(bb);
+//		}
+		// FIXME optimize to read as much as possible at once
+		while (!isEmpty() && buf.hasRemaining()) {
+			buf.put(readByte());
+		}
+	}
 	public byte readByte() throws IOException {
 		throw new UnsupportedOperationException();
 	}
-}
\ No newline at end of file
+
+	// XXX decide whether may or may not change position in the DataAccess
+	// FIXME exception handling is not right, just for the sake of quick test
+	public byte[] byteArray() throws IOException {
+		reset();
+		byte[] rv = new byte[(int) length()];
+		readBytes(rv, 0, rv.length);
+		return rv;
+	}
+}