Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/ByteArrayDataAccess.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/ByteArrayDataAccess.java@9429c7bd1920 |
children | b413b16d10a5 |
comparison
equal
deleted
inserted
replaced
156:643ddec3be36 | 157:d5268ca7715b |
---|---|
1 /* | |
2 * Copyright (c) 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 | |
21 | |
22 /** | |
23 * | |
24 * @author Artem Tikhomirov | |
25 * @author TMate Software Ltd. | |
26 */ | |
27 public class ByteArrayDataAccess extends DataAccess { | |
28 | |
29 private final byte[] data; | |
30 private final int offset; | |
31 private final int length; | |
32 private int pos; | |
33 | |
34 public ByteArrayDataAccess(byte[] data) { | |
35 this(data, 0, data.length); | |
36 } | |
37 | |
38 public ByteArrayDataAccess(byte[] data, int offset, int length) { | |
39 this.data = data; | |
40 this.offset = offset; | |
41 this.length = length; | |
42 pos = 0; | |
43 } | |
44 | |
45 @Override | |
46 public byte readByte() throws IOException { | |
47 if (pos >= length) { | |
48 throw new IOException(); | |
49 } | |
50 return data[offset + pos++]; | |
51 } | |
52 @Override | |
53 public void readBytes(byte[] buf, int off, int len) throws IOException { | |
54 if (len > (this.length - pos)) { | |
55 throw new IOException(); | |
56 } | |
57 System.arraycopy(data, pos, buf, off, len); | |
58 pos += len; | |
59 } | |
60 | |
61 @Override | |
62 public ByteArrayDataAccess reset() { | |
63 pos = 0; | |
64 return this; | |
65 } | |
66 @Override | |
67 public long length() { | |
68 return length; | |
69 } | |
70 @Override | |
71 public void seek(long offset) { | |
72 pos = (int) offset; | |
73 } | |
74 @Override | |
75 public void skip(int bytes) throws IOException { | |
76 seek(pos + bytes); | |
77 } | |
78 @Override | |
79 public boolean isEmpty() { | |
80 return pos >= length; | |
81 } | |
82 | |
83 // | |
84 | |
85 // when byte[] needed from DA, we may save few cycles and some memory giving this (otherwise unsafe) access to underlying data | |
86 @Override | |
87 public byte[] byteArray() { | |
88 return data; | |
89 } | |
90 } |