comparison src/org/tmatesoft/hg/internal/DataAccessInputStream.java @ 673:545b1d4cc11d

Refactor HgBundle.GroupElement (clear experimental mark), resolve few technical debt issues
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 12 Jul 2013 20:14:24 +0200
parents
children
comparison
equal deleted inserted replaced
672:d2552e6a5af6 673:545b1d4cc11d
1 /*
2 * Copyright (c) 2013 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.io.InputStream;
21
22 /**
23 * Wrap our internal API into a public one
24 *
25 * @author Artem Tikhomirov
26 * @author TMate Software Ltd.
27 */
28 public class DataAccessInputStream extends InputStream {
29
30 private final DataAccess da;
31 private int bytesLeft = -1;
32
33 public DataAccessInputStream(DataAccess dataAccess) {
34 da = dataAccess;
35 }
36
37 @Override
38 public int available() throws IOException {
39 initAvailable();
40 return bytesLeft;
41 }
42
43 @Override
44 public int read() throws IOException {
45 initAvailable();
46 if (bytesLeft == 0) {
47 return -1;
48 }
49 int rv = da.readByte();
50 bytesLeft--;
51 return rv;
52 }
53
54 @Override
55 public int read(byte[] b, int off, int len) throws IOException {
56 initAvailable();
57 if (bytesLeft == 0) {
58 return -1;
59 }
60 if (len == 0) {
61 return 0;
62 }
63 int x = Math.min(len, bytesLeft);
64 da.readBytes(b, off, x);
65 bytesLeft -= x;
66 return x;
67 }
68
69
70 private void initAvailable() throws IOException {
71 da.reset();
72 if (bytesLeft == -1) {
73 bytesLeft = da.length();
74 }
75 }
76 }