Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/DeflaterDataSerializer.java @ 534:243202f1bda5
Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 04 Feb 2013 18:00:55 +0100 |
parents | |
children | 7c0d2ce340b8 |
comparison
equal
deleted
inserted
replaced
533:e6f72c9829a6 | 534:243202f1bda5 |
---|---|
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.util.zip.Deflater; | |
21 import java.util.zip.DeflaterOutputStream; | |
22 | |
23 /** | |
24 * {@link DeflaterOutputStream} counterpart for {@link DataSerializer} API | |
25 * | |
26 * @author Artem Tikhomirov | |
27 * @author TMate Software Ltd. | |
28 */ | |
29 class DeflaterDataSerializer extends DataSerializer { | |
30 | |
31 private static final int AUX_BUFFER_CAPACITY = 5; // room for 5 ints | |
32 | |
33 private final DataSerializer delegate; | |
34 private final Deflater deflater; | |
35 private final byte[] deflateOutBuffer; | |
36 private final byte[] auxBuffer; | |
37 | |
38 public DeflaterDataSerializer(DataSerializer delegateSerializer, Deflater def, int bufferSizeHint) { | |
39 delegate = delegateSerializer; | |
40 deflater = def; | |
41 deflateOutBuffer = new byte[bufferSizeHint <= 0 ? 2048 : bufferSizeHint]; | |
42 auxBuffer = new byte[4 * AUX_BUFFER_CAPACITY]; // sizeof(int) * capacity | |
43 } | |
44 | |
45 @Override | |
46 public void writeInt(int... values) throws IOException { | |
47 for (int i = 0; i < values.length; i+= AUX_BUFFER_CAPACITY) { | |
48 int idx = 0; | |
49 for (int j = i, x = Math.min(values.length, i + AUX_BUFFER_CAPACITY); j < x; j++) { | |
50 int v = values[j]; | |
51 auxBuffer[idx++] = (byte) ((v >>> 24) & 0x0ff); | |
52 auxBuffer[idx++] = (byte) ((v >>> 16) & 0x0ff); | |
53 auxBuffer[idx++] = (byte) ((v >>> 8) & 0x0ff); | |
54 auxBuffer[idx++] = (byte) (v & 0x0ff); | |
55 } | |
56 internalWrite(auxBuffer, 0, idx); | |
57 } | |
58 } | |
59 | |
60 @Override | |
61 public void write(byte[] data, int offset, int length) throws IOException { | |
62 // @see DeflaterOutputStream#write(byte[], int, int) | |
63 int stride = deflateOutBuffer.length; | |
64 for (int i = 0; i < length; i += stride) { | |
65 internalWrite(data, offset + i, Math.min(stride, length - i)); | |
66 } | |
67 } | |
68 | |
69 private void internalWrite(byte[] data, int offset, int length) throws IOException { | |
70 deflater.setInput(data, offset, length); | |
71 while (!deflater.needsInput()) { | |
72 deflate(); | |
73 } | |
74 } | |
75 | |
76 @Override | |
77 public void done() { | |
78 delegate.done(); | |
79 } | |
80 | |
81 public void finish() throws IOException { | |
82 if (!deflater.finished()) { | |
83 deflater.finish(); | |
84 while (!deflater.finished()) { | |
85 deflate(); | |
86 } | |
87 } | |
88 } | |
89 | |
90 protected void deflate() throws IOException { | |
91 int len = deflater.deflate(deflateOutBuffer, 0, deflateOutBuffer.length); | |
92 if (len > 0) { | |
93 delegate.write(deflateOutBuffer, 0, len); | |
94 } | |
95 } | |
96 } |