comparison src/org/tmatesoft/hg/internal/DeflaterDataSerializer.java @ 618:7c0d2ce340b8

Refactor approach how content finds it way down to a commit revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 May 2013 19:46:13 +0200
parents 243202f1bda5
children
comparison
equal deleted inserted replaced
617:65c01508f002 618:7c0d2ce340b8
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.IOException;
20 import java.util.zip.Deflater; 19 import java.util.zip.Deflater;
21 import java.util.zip.DeflaterOutputStream; 20 import java.util.zip.DeflaterOutputStream;
21
22 import org.tmatesoft.hg.core.HgIOException;
22 23
23 /** 24 /**
24 * {@link DeflaterOutputStream} counterpart for {@link DataSerializer} API 25 * {@link DeflaterOutputStream} counterpart for {@link DataSerializer} API
25 * 26 *
26 * @author Artem Tikhomirov 27 * @author Artem Tikhomirov
41 deflateOutBuffer = new byte[bufferSizeHint <= 0 ? 2048 : bufferSizeHint]; 42 deflateOutBuffer = new byte[bufferSizeHint <= 0 ? 2048 : bufferSizeHint];
42 auxBuffer = new byte[4 * AUX_BUFFER_CAPACITY]; // sizeof(int) * capacity 43 auxBuffer = new byte[4 * AUX_BUFFER_CAPACITY]; // sizeof(int) * capacity
43 } 44 }
44 45
45 @Override 46 @Override
46 public void writeInt(int... values) throws IOException { 47 public void writeInt(int... values) throws HgIOException {
47 for (int i = 0; i < values.length; i+= AUX_BUFFER_CAPACITY) { 48 for (int i = 0; i < values.length; i+= AUX_BUFFER_CAPACITY) {
48 int idx = 0; 49 int idx = 0;
49 for (int j = i, x = Math.min(values.length, i + AUX_BUFFER_CAPACITY); j < x; j++) { 50 for (int j = i, x = Math.min(values.length, i + AUX_BUFFER_CAPACITY); j < x; j++) {
50 int v = values[j]; 51 int v = values[j];
51 auxBuffer[idx++] = (byte) ((v >>> 24) & 0x0ff); 52 auxBuffer[idx++] = (byte) ((v >>> 24) & 0x0ff);
56 internalWrite(auxBuffer, 0, idx); 57 internalWrite(auxBuffer, 0, idx);
57 } 58 }
58 } 59 }
59 60
60 @Override 61 @Override
61 public void write(byte[] data, int offset, int length) throws IOException { 62 public void write(byte[] data, int offset, int length) throws HgIOException {
62 // @see DeflaterOutputStream#write(byte[], int, int) 63 // @see DeflaterOutputStream#write(byte[], int, int)
63 int stride = deflateOutBuffer.length; 64 int stride = deflateOutBuffer.length;
64 for (int i = 0; i < length; i += stride) { 65 for (int i = 0; i < length; i += stride) {
65 internalWrite(data, offset + i, Math.min(stride, length - i)); 66 internalWrite(data, offset + i, Math.min(stride, length - i));
66 } 67 }
67 } 68 }
68 69
69 private void internalWrite(byte[] data, int offset, int length) throws IOException { 70 private void internalWrite(byte[] data, int offset, int length) throws HgIOException {
70 deflater.setInput(data, offset, length); 71 deflater.setInput(data, offset, length);
71 while (!deflater.needsInput()) { 72 while (!deflater.needsInput()) {
72 deflate(); 73 deflate();
73 } 74 }
74 } 75 }
75 76
76 @Override 77 @Override
77 public void done() { 78 public void done() throws HgIOException {
78 delegate.done(); 79 delegate.done();
79 } 80 }
80 81
81 public void finish() throws IOException { 82 public void finish() throws HgIOException {
82 if (!deflater.finished()) { 83 if (!deflater.finished()) {
83 deflater.finish(); 84 deflater.finish();
84 while (!deflater.finished()) { 85 while (!deflater.finished()) {
85 deflate(); 86 deflate();
86 } 87 }
87 } 88 }
88 } 89 }
89 90
90 protected void deflate() throws IOException { 91 protected void deflate() throws HgIOException {
91 int len = deflater.deflate(deflateOutBuffer, 0, deflateOutBuffer.length); 92 int len = deflater.deflate(deflateOutBuffer, 0, deflateOutBuffer.length);
92 if (len > 0) { 93 if (len > 0) {
93 delegate.write(deflateOutBuffer, 0, len); 94 delegate.write(deflateOutBuffer, 0, len);
94 } 95 }
95 } 96 }