comparison hg4j/src/main/java/org/tmatesoft/hg/internal/FilterDataAccess.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) 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 * XXX Perhaps, DataAccessSlice? Unlike FilterInputStream, we limit amount of data read from DataAccess being filtered.
24 *
25 *
26 * @author Artem Tikhomirov
27 * @author TMate Software Ltd.
28 */
29 public class FilterDataAccess extends DataAccess {
30 private final DataAccess dataAccess;
31 private final int offset;
32 private final int length;
33 private int count;
34
35 public FilterDataAccess(DataAccess dataAccess, int offset, int length) {
36 this.dataAccess = dataAccess;
37 this.offset = offset;
38 this.length = length;
39 count = length;
40 }
41
42 protected int available() {
43 return count;
44 }
45
46 @Override
47 public FilterDataAccess reset() throws IOException {
48 count = length;
49 return this;
50 }
51
52 @Override
53 public boolean isEmpty() {
54 return count <= 0;
55 }
56
57 @Override
58 public int length() {
59 return length;
60 }
61
62 @Override
63 public void seek(int localOffset) throws IOException {
64 if (localOffset < 0 || localOffset > length) {
65 throw new IllegalArgumentException();
66 }
67 dataAccess.seek(offset + localOffset);
68 count = (int) (length - localOffset);
69 }
70
71 @Override
72 public void skip(int bytes) throws IOException {
73 int newCount = count - bytes;
74 if (newCount < 0 || newCount > length) {
75 throw new IllegalArgumentException();
76 }
77 seek(length - newCount);
78 /*
79 can't use next code because don't want to rewind backing DataAccess on reset()
80 i.e. this.reset() modifies state of this instance only, while filtered DA may go further.
81 Only actual this.skip/seek/read would rewind it to desired position
82 dataAccess.skip(bytes);
83 count = newCount;
84 */
85
86 }
87
88 @Override
89 public byte readByte() throws IOException {
90 if (count <= 0) {
91 throw new IllegalArgumentException("Underflow"); // XXX be descriptive
92 }
93 if (count == length) {
94 dataAccess.seek(offset);
95 }
96 count--;
97 return dataAccess.readByte();
98 }
99
100 @Override
101 public void readBytes(byte[] b, int off, int len) throws IOException {
102 if (len == 0) {
103 return;
104 }
105 if (count <= 0 || len > count) {
106 throw new IllegalArgumentException(String.format("Underflow. Bytes left: %d, asked to read %d", count, len));
107 }
108 if (count == length) {
109 dataAccess.seek(offset);
110 }
111 dataAccess.readBytes(b, off, len);
112 count -= len;
113 }
114
115 // done shall be no-op, as we have no idea what's going on with DataAccess we filter
116 }