comparison src/org/tmatesoft/hg/internal/DataSerializer.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 e55f17a7a195
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; 19 import java.io.ByteArrayOutputStream;
20
21 import org.tmatesoft.hg.core.HgIOException;
20 22
21 /** 23 /**
22 * Serialization friend of {@link DataAccess} 24 * Serialization friend of {@link DataAccess}
23 * 25 *
24 * @author Artem Tikhomirov 26 * @author Artem Tikhomirov
25 * @author TMate Software Ltd. 27 * @author TMate Software Ltd.
26 */ 28 */
27 @Experimental(reason="Work in progress") 29 @Experimental(reason="Work in progress")
28 public class DataSerializer { 30 public class DataSerializer {
31 private byte[] buffer;
29 32
30 public void writeByte(byte... values) throws IOException { 33 public void writeByte(byte... values) throws HgIOException {
31 write(values, 0, values.length); 34 write(values, 0, values.length);
32 } 35 }
33 36
34 public void writeInt(int... values) throws IOException { 37 public void writeInt(int... values) throws HgIOException {
35 byte[] buf = new byte[4]; 38 ensureBufferSize(4*values.length); // sizeof(int)
39 int idx = 0;
36 for (int v : values) { 40 for (int v : values) {
37 bigEndian(v, buf, 0); 41 bigEndian(v, buffer, idx);
38 write(buf, 0, buf.length); 42 idx += 4;
43 }
44 write(buffer, 0, idx);
45 }
46
47 public void write(byte[] data, int offset, int length) throws HgIOException {
48 throw new HgIOException("Attempt to write to non-existent file", null);
49 }
50
51 public void done() throws HgIOException {
52 // no-op
53 }
54
55 private void ensureBufferSize(int bytesNeeded) {
56 if (buffer == null || buffer.length < bytesNeeded) {
57 buffer = new byte[bytesNeeded];
39 } 58 }
40 } 59 }
41 60
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 /** 61 /**
52 * Writes 4 bytes of supplied value into the buffer at given offset, big-endian. 62 * Writes 4 bytes of supplied value into the buffer at given offset, big-endian.
53 */ 63 */
54 public static final void bigEndian(int value, byte[] buffer, int offset) { 64 public static final void bigEndian(int value, byte[] buffer, int offset) {
55 assert offset + 4 <= buffer.length; 65 assert offset + 4 <= buffer.length;
62 /** 72 /**
63 * Denotes an entity that wants to/could be serialized 73 * Denotes an entity that wants to/could be serialized
64 */ 74 */
65 @Experimental(reason="Work in progress") 75 @Experimental(reason="Work in progress")
66 interface DataSource { 76 interface DataSource {
67 public void serialize(DataSerializer out) throws IOException; 77 /**
78 * Invoked once for a single write operation,
79 * although the source itself may get serialized several times
80 */
81 public void serialize(DataSerializer out) throws HgIOException;
68 82
69 /** 83 /**
70 * Hint of data length it would like to writes 84 * Hint of data length it would like to writes
71 * @return -1 if can't answer 85 * @return -1 if can't answer
72 */ 86 */
79 93
80 public ByteArrayDataSource(byte[] bytes) { 94 public ByteArrayDataSource(byte[] bytes) {
81 data = bytes; 95 data = bytes;
82 } 96 }
83 97
84 public void serialize(DataSerializer out) throws IOException { 98 public void serialize(DataSerializer out) throws HgIOException {
85 if (data != null) { 99 if (data != null) {
86 out.write(data, 0, data.length); 100 out.write(data, 0, data.length);
87 } 101 }
88 } 102 }
89 103
90 public int serializeLength() { 104 public int serializeLength() {
91 return data == null ? 0 : data.length; 105 return data == null ? 0 : data.length;
92 } 106 }
107 }
108
109 public static class ByteArrayDataSerializer extends DataSerializer {
110 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
111
112 @Override
113 public void write(byte[] data, int offset, int length) {
114 out.write(data, offset, length);
115 }
93 116
117 public byte[] toByteArray() {
118 return out.toByteArray();
119 }
94 } 120 }
95 } 121 }