comparison src/org/tmatesoft/hg/internal/BundleSerializer.java @ 673:545b1d4cc11d

Refactor HgBundle.GroupElement (clear experimental mark), resolve few technical debt issues
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 12 Jul 2013 20:14:24 +0200
parents
children
comparison
equal deleted inserted replaced
672:d2552e6a5af6 673:545b1d4cc11d
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.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import org.tmatesoft.hg.core.HgIOException;
24 import org.tmatesoft.hg.core.SessionContext;
25 import org.tmatesoft.hg.repo.HgBundle;
26 import org.tmatesoft.hg.repo.HgInternals;
27 import org.tmatesoft.hg.repo.HgRuntimeException;
28 import org.tmatesoft.hg.util.LogFacility;
29
30 /**
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class BundleSerializer implements DataSerializer.DataSource {
36 private final LogFacility log;
37 private final File bundleFile;
38
39 public static BundleSerializer newInstance(SessionContext ctx, HgBundle bundle) {
40 return new BundleSerializer(ctx.getLog(), HgInternals.getBundleFile(bundle));
41 }
42
43 public BundleSerializer(LogFacility logFacility, File bundleFile) {
44 log = logFacility;
45 this.bundleFile = bundleFile;
46 }
47
48 public void serialize(DataSerializer out) throws HgIOException, HgRuntimeException {
49 FileInputStream fis = null;
50 try {
51 fis = new FileInputStream(bundleFile);
52 byte[] buffer = new byte[8*1024];
53 int r;
54 while ((r = fis.read(buffer, 0, buffer.length)) > 0) {
55 out.write(buffer, 0, r);
56 }
57
58 } catch (IOException ex) {
59 throw new HgIOException("Failed to serialize bundle", bundleFile);
60 } finally {
61 new FileUtils(log, this).closeQuietly(fis, bundleFile);
62 }
63 }
64
65 public int serializeLength() throws HgRuntimeException {
66 return Internals.ltoi(bundleFile.length());
67 }
68 }