Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/DataSerializer.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 | e55f17a7a195 |
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 | |
21 /** | |
22 * Serialization friend of {@link DataAccess} | |
23 * | |
24 * @author Artem Tikhomirov | |
25 * @author TMate Software Ltd. | |
26 */ | |
27 @Experimental(reason="Work in progress") | |
28 public class DataSerializer { | |
29 | |
30 public void writeByte(byte... values) throws IOException { | |
31 write(values, 0, values.length); | |
32 } | |
33 | |
34 public void writeInt(int... values) throws IOException { | |
35 byte[] buf = new byte[4]; | |
36 for (int v : values) { | |
37 bigEndian(v, buf, 0); | |
38 write(buf, 0, buf.length); | |
39 } | |
40 } | |
41 | |
42 public void write(byte[] data, int offset, int length) throws IOException { | |
43 throw new IOException("Attempt to write to non-existent file"); | |
44 } | |
45 | |
46 public void done() { | |
47 // FIXME perhaps, shall allow IOException, too | |
48 // no-op | |
49 } | |
50 | |
51 /** | |
52 * Writes 4 bytes of supplied value into the buffer at given offset, big-endian. | |
53 */ | |
54 public static final void bigEndian(int value, byte[] buffer, int offset) { | |
55 assert offset + 4 <= buffer.length; | |
56 buffer[offset++] = (byte) ((value >>> 24) & 0x0ff); | |
57 buffer[offset++] = (byte) ((value >>> 16) & 0x0ff); | |
58 buffer[offset++] = (byte) ((value >>> 8) & 0x0ff); | |
59 buffer[offset++] = (byte) (value & 0x0ff); | |
60 } | |
61 | |
62 @Experimental(reason="Work in progress") | |
63 interface DataSource { | |
64 public void serialize(DataSerializer out) throws IOException; | |
65 | |
66 /** | |
67 * Hint of data length it would like to writes | |
68 * @return -1 if can't answer | |
69 */ | |
70 public int serializeLength(); | |
71 } | |
72 | |
73 public static class ByteArrayDataSource implements DataSource { | |
74 | |
75 private final byte[] data; | |
76 | |
77 public ByteArrayDataSource(byte[] bytes) { | |
78 data = bytes; | |
79 } | |
80 | |
81 public void serialize(DataSerializer out) throws IOException { | |
82 if (data != null) { | |
83 out.write(data, 0, data.length); | |
84 } | |
85 } | |
86 | |
87 public int serializeLength() { | |
88 return data == null ? 0 : data.length; | |
89 } | |
90 | |
91 } | |
92 } |