Mercurial > jhg
comparison src/com/tmate/hgkit/fs/ByteArrayDataAccess.java @ 51:9429c7bd1920 wrap-data-access
Try DataAccess to reach revision data instead of plain byte arrays
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sun, 16 Jan 2011 01:20:26 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
50:f1db8610da62 | 51:9429c7bd1920 |
---|---|
1 /* | |
2 * Copyright (c) 2011 Artem Tikhomirov | |
3 */ | |
4 package com.tmate.hgkit.fs; | |
5 | |
6 import java.io.IOException; | |
7 | |
8 /** | |
9 * | |
10 * @author artem | |
11 */ | |
12 public class ByteArrayDataAccess extends DataAccess { | |
13 | |
14 private final byte[] data; | |
15 private final int offset; | |
16 private final int length; | |
17 private int pos; | |
18 | |
19 public ByteArrayDataAccess(byte[] data) { | |
20 this(data, 0, data.length); | |
21 } | |
22 | |
23 public ByteArrayDataAccess(byte[] data, int offset, int length) { | |
24 this.data = data; | |
25 this.offset = offset; | |
26 this.length = length; | |
27 pos = 0; | |
28 } | |
29 | |
30 @Override | |
31 public byte readByte() throws IOException { | |
32 if (pos >= length) { | |
33 throw new IOException(); | |
34 } | |
35 return data[offset + pos++]; | |
36 } | |
37 @Override | |
38 public void readBytes(byte[] buf, int off, int len) throws IOException { | |
39 if (len > (this.length - pos)) { | |
40 throw new IOException(); | |
41 } | |
42 System.arraycopy(data, pos, buf, off, len); | |
43 pos += len; | |
44 } | |
45 | |
46 @Override | |
47 public void reset() { | |
48 pos = 0; | |
49 } | |
50 @Override | |
51 public long length() { | |
52 return length; | |
53 } | |
54 @Override | |
55 public void seek(long offset) { | |
56 pos = (int) offset; | |
57 } | |
58 @Override | |
59 public void skip(int bytes) throws IOException { | |
60 seek(pos + bytes); | |
61 } | |
62 @Override | |
63 public boolean isEmpty() { | |
64 return pos >= length; | |
65 } | |
66 | |
67 // | |
68 | |
69 // when byte[] needed from DA, we may save few cycles and some memory giving this (otherwise unsafe) access to underlying data | |
70 @Override | |
71 public byte[] byteArray() { | |
72 return data; | |
73 } | |
74 } |