comparison hg4j/src/main/java/org/tmatesoft/hg/internal/DataAccess.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2010-2011 TMate Software Ltd
3 *
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
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21
22 /**
23 * relevant parts of DataInput, non-stream nature (seek operation), explicit check for end of data.
24 * convenient skip (+/- bytes)
25 * Primary goal - effective file read, so that clients don't need to care whether to call few
26 * distinct getInt() or readBytes(totalForFewInts) and parse themselves instead in an attempt to optimize.
27 *
28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd.
30 */
31 public class DataAccess {
32 public boolean isEmpty() {
33 return true;
34 }
35 public int 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;
46 }
47 // absolute positioning
48 public void seek(int offset) throws IOException {
49 throw new UnsupportedOperationException();
50 }
51 // relative positioning
52 public void skip(int bytes) throws IOException {
53 throw new UnsupportedOperationException();
54 }
55 // shall be called once this object no longer needed
56 public void done() {
57 // no-op in this empty implementation
58 }
59 public int readInt() throws IOException {
60 byte[] b = new byte[4];
61 readBytes(b, 0, 4);
62 return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
63 }
64 public long readLong() throws IOException {
65 byte[] b = new byte[8];
66 readBytes(b, 0, 8);
67 int i1 = b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
68 int i2 = b[4] << 24 | (b[5] & 0xFF) << 16 | (b[6] & 0xFF) << 8 | (b[7] & 0xFF);
69 return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl);
70 }
71 public void readBytes(byte[] buf, int offset, int length) throws IOException {
72 throw new UnsupportedOperationException();
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 }
90 public byte readByte() throws IOException {
91 throw new UnsupportedOperationException();
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[length()];
99 readBytes(rv, 0, rv.length);
100 return rv;
101 }
102 }