Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/RevlogCompressor.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 | 6526d8adbc0f |
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 | 20 |
| 21 import org.tmatesoft.hg.core.HgIOException; | |
| 22 import org.tmatesoft.hg.core.SessionContext; | 22 import org.tmatesoft.hg.core.SessionContext; |
| 23 import org.tmatesoft.hg.util.LogFacility.Severity; | 23 import org.tmatesoft.hg.util.LogFacility.Severity; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * | 26 * |
| 42 sourceData = source; | 42 sourceData = source; |
| 43 compressedLen = -1; | 43 compressedLen = -1; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // out stream is not closed! | 46 // out stream is not closed! |
| 47 public int writeCompressedData(DataSerializer out) throws IOException { | 47 public int writeCompressedData(DataSerializer out) throws HgIOException { |
| 48 zip.reset(); | 48 zip.reset(); |
| 49 DeflaterDataSerializer dds = new DeflaterDataSerializer(out, zip, sourceData.serializeLength()); | 49 DeflaterDataSerializer dds = new DeflaterDataSerializer(out, zip, sourceData.serializeLength()); |
| 50 sourceData.serialize(dds); | 50 sourceData.serialize(dds); |
| 51 dds.finish(); | 51 dds.finish(); |
| 52 return zip.getTotalOut(); | 52 return zip.getTotalOut(); |
| 59 Counter counter = new Counter(); | 59 Counter counter = new Counter(); |
| 60 try { | 60 try { |
| 61 compressedLen = writeCompressedData(counter); | 61 compressedLen = writeCompressedData(counter); |
| 62 assert counter.totalWritten == compressedLen; | 62 assert counter.totalWritten == compressedLen; |
| 63 return compressedLen; | 63 return compressedLen; |
| 64 } catch (IOException ex) { | 64 } catch (HgIOException ex) { |
| 65 // can't happen provided we write to our stream that does nothing but byte counting | 65 // can't happen provided we write to our stream that does nothing but byte counting |
| 66 ctx.getLog().dump(getClass(), Severity.Error, ex, "Failed estimating compressed length of revlog data"); | 66 ctx.getLog().dump(getClass(), Severity.Error, ex, "Failed estimating compressed length of revlog data"); |
| 67 return counter.totalWritten; // use best known value so far | 67 return counter.totalWritten; // use best known value so far |
| 68 } | 68 } |
| 69 } | 69 } |
| 70 | 70 |
| 71 private static class Counter extends DataSerializer { | 71 private static class Counter extends DataSerializer { |
| 72 public int totalWritten = 0; | 72 public int totalWritten = 0; |
| 73 | 73 |
| 74 public void writeByte(byte... values) throws IOException { | 74 public void writeByte(byte... values) throws HgIOException { |
| 75 totalWritten += values.length; | 75 totalWritten += values.length; |
| 76 } | 76 } |
| 77 | 77 |
| 78 public void writeInt(int... values) throws IOException { | 78 public void writeInt(int... values) throws HgIOException { |
| 79 totalWritten += 4 * values.length; | 79 totalWritten += 4 * values.length; |
| 80 } | 80 } |
| 81 | 81 |
| 82 public void write(byte[] data, int offset, int length) throws IOException { | 82 public void write(byte[] data, int offset, int length) throws HgIOException { |
| 83 totalWritten += length; | 83 totalWritten += length; |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 } | 86 } |
