comparison 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
comparison
equal deleted inserted replaced
156:643ddec3be36 157:d5268ca7715b
1 /* 1 /*
2 * Copyright (c) 2010 TMate Software Ltd 2 * Copyright (c) 2010-2011 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.IOException; 19 import java.io.IOException;
20 import java.nio.ByteBuffer;
20 21
21 /** 22 /**
22 * relevant parts of DataInput, non-stream nature (seek operation), explicit check for end of data. 23 * relevant parts of DataInput, non-stream nature (seek operation), explicit check for end of data.
23 * convenient skip (+/- bytes) 24 * convenient skip (+/- bytes)
24 * Primary goal - effective file read, so that clients don't need to care whether to call few 25 * Primary goal - effective file read, so that clients don't need to care whether to call few
28 * @author TMate Software Ltd. 29 * @author TMate Software Ltd.
29 */ 30 */
30 public class DataAccess { 31 public class DataAccess {
31 public boolean isEmpty() { 32 public boolean isEmpty() {
32 return true; 33 return true;
34 }
35 public long length() {
36 return 0;
37 }
38 /**
39 * get this instance into initial state
40 * @throws IOException
41 * @return <code>this</code> for convenience
42 */
43 public DataAccess reset() throws IOException {
44 // nop, empty instance is always in the initial state
45 return this;
33 } 46 }
34 // absolute positioning 47 // absolute positioning
35 public void seek(long offset) throws IOException { 48 public void seek(long offset) throws IOException {
36 throw new UnsupportedOperationException(); 49 throw new UnsupportedOperationException();
37 } 50 }
56 return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl); 69 return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl);
57 } 70 }
58 public void readBytes(byte[] buf, int offset, int length) throws IOException { 71 public void readBytes(byte[] buf, int offset, int length) throws IOException {
59 throw new UnsupportedOperationException(); 72 throw new UnsupportedOperationException();
60 } 73 }
74 // reads bytes into ByteBuffer, up to its limit or total data length, whichever smaller
75 // FIXME perhaps, in DataAccess paradigm (when we read known number of bytes, we shall pass specific byte count to read)
76 public void readBytes(ByteBuffer buf) throws IOException {
77 // int toRead = Math.min(buf.remaining(), (int) length());
78 // if (buf.hasArray()) {
79 // readBytes(buf.array(), buf.arrayOffset(), toRead);
80 // } else {
81 // byte[] bb = new byte[toRead];
82 // readBytes(bb, 0, bb.length);
83 // buf.put(bb);
84 // }
85 // FIXME optimize to read as much as possible at once
86 while (!isEmpty() && buf.hasRemaining()) {
87 buf.put(readByte());
88 }
89 }
61 public byte readByte() throws IOException { 90 public byte readByte() throws IOException {
62 throw new UnsupportedOperationException(); 91 throw new UnsupportedOperationException();
63 } 92 }
93
94 // XXX decide whether may or may not change position in the DataAccess
95 // FIXME exception handling is not right, just for the sake of quick test
96 public byte[] byteArray() throws IOException {
97 reset();
98 byte[] rv = new byte[(int) length()];
99 readBytes(rv, 0, rv.length);
100 return rv;
101 }
64 } 102 }