annotate src/org/tmatesoft/hg/internal/ByteVector.java @ 621:99ad1e3a4e4d

RevlogStream: be aware of existence (not HgDataFile), facilitate use of an added HgDataFile over a commit; Rollback: be more sensitive about file changes (file size is not enough: write/rollback leaves it intact); tests
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 18 May 2013 22:23:57 +0200
parents 88afffd39899
children a62079bc422b
rev   line source
574
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.ByteArrayOutputStream;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 /**
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 * Alternative to {@link ByteArrayOutputStream}, with extra operation that prevent extra byte[] instances
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 *
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 * @author Artem Tikhomirov
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 * @author TMate Software Ltd.
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 */
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 public class ByteVector {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 private byte[] data;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 private int count;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 private final int increment;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 public ByteVector(int initialSize, int increment) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 data = new byte[initialSize];
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 this.increment = increment;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public void add(int b) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 if (count == data.length) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 byte[] newData = new byte[count + increment];
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 System.arraycopy(data, 0, newData, 0, count);
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 data = newData;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 data[count++] = (byte) b;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 public int size() {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 return count;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 public void clear() {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 count = 0;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 public boolean equalsTo(byte[] array) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 if (array == null || array.length != count) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 return false;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 for (int i = 0; i < count; i++) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 if (data[i] != array[i]) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 return false;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 return true;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 /**
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 * Copies content of this vector into destination array.
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 * @param destination array, greater or equal to {@link #size()} of the vector
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 */
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 public void copyTo(byte[] destination) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 if (destination == null || destination.length < count) {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 throw new IllegalArgumentException();
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 System.arraycopy(data, 0, destination, 0, count);
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 public byte[] toByteArray() {
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 byte[] rv = new byte[count];
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 copyTo(rv);
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 return rv;
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
88afffd39899 Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 }