Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/FilterDataAccess.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/FilterDataAccess.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 * 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 long offset; | |
32 private final int length; | |
33 private int count; | |
34 | |
35 public FilterDataAccess(DataAccess dataAccess, long 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 long length() { | |
59 return length; | |
60 } | |
61 | |
62 @Override | |
63 public void seek(long 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 (count <= 0 || len > count) { | |
103 throw new IllegalArgumentException("Underflow"); // XXX be descriptive | |
104 } | |
105 if (count == length) { | |
106 dataAccess.seek(offset); | |
107 } | |
108 dataAccess.readBytes(b, off, len); | |
109 count -= len; | |
110 } | |
111 | |
112 // done shall be no-op, as we have no idea what's going on with DataAccess we filter | |
113 } |