Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/RevlogCompressor.java @ 530:0f6fa88e2162
Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 23 Jan 2013 17:46:12 +0100 |
parents | |
children | 243202f1bda5 |
comparison
equal
deleted
inserted
replaced
529:95bdcf75e71e | 530:0f6fa88e2162 |
---|---|
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.OutputStream; | |
21 import java.util.zip.Deflater; | |
22 import java.util.zip.DeflaterOutputStream; | |
23 | |
24 /** | |
25 * | |
26 * @author Artem Tikhomirov | |
27 * @author TMate Software Ltd. | |
28 */ | |
29 public class RevlogCompressor { | |
30 private final Deflater zip; | |
31 private byte[] sourceData; | |
32 private int compressedLenEstimate; | |
33 | |
34 public RevlogCompressor() { | |
35 zip = new Deflater(); | |
36 } | |
37 | |
38 public void reset(byte[] source) { | |
39 sourceData = source; | |
40 compressedLenEstimate = -1; | |
41 } | |
42 | |
43 public int writeCompressedData(OutputStream out) throws IOException { | |
44 zip.reset(); | |
45 DeflaterOutputStream dos = new DeflaterOutputStream(out, zip, Math.min(2048, sourceData.length)); | |
46 dos.write(sourceData); | |
47 dos.finish(); | |
48 return zip.getTotalOut(); | |
49 } | |
50 | |
51 public int getCompressedLengthEstimate() { | |
52 if (compressedLenEstimate != -1) { | |
53 return compressedLenEstimate; | |
54 } | |
55 zip.reset(); | |
56 int rv = 0; | |
57 // from DeflaterOutputStream: | |
58 byte[] buffer = new byte[Math.min(2048, sourceData.length)]; | |
59 for (int i = 0, stride = buffer.length; i < sourceData.length; i+= stride) { | |
60 zip.setInput(sourceData, i, Math.min(stride, sourceData.length - i)); | |
61 while (!zip.needsInput()) { | |
62 rv += zip.deflate(buffer, 0, buffer.length); | |
63 } | |
64 } | |
65 zip.finish(); | |
66 while (!zip.finished()) { | |
67 rv += zip.deflate(buffer, 0, buffer.length); | |
68 } | |
69 return compressedLenEstimate = rv; | |
70 } | |
71 } |