comparison src/org/tmatesoft/hg/internal/DataSerializer.java @ 645:14dac192aa26

Push: phase2 - upload bundle with changes to remote server
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 20 Jun 2013 19:15:09 +0200
parents 6526d8adbc0f
children 545b1d4cc11d
comparison
equal deleted inserted replaced
644:1deea2f33218 645:14dac192aa26
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.ByteArrayOutputStream; 19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
20 22
21 import org.tmatesoft.hg.core.HgIOException; 23 import org.tmatesoft.hg.core.HgIOException;
22 import org.tmatesoft.hg.repo.HgRuntimeException; 24 import org.tmatesoft.hg.repo.HgRuntimeException;
23 25
24 /** 26 /**
72 74
73 /** 75 /**
74 * Denotes an entity that wants to/could be serialized 76 * Denotes an entity that wants to/could be serialized
75 */ 77 */
76 @Experimental(reason="Work in progress") 78 @Experimental(reason="Work in progress")
77 interface DataSource { 79 public interface DataSource {
78 /** 80 /**
79 * Invoked once for a single write operation, 81 * Invoked once for a single write operation,
80 * although the source itself may get serialized several times 82 * although the source itself may get serialized several times
81 */ 83 */
82 public void serialize(DataSerializer out) throws HgIOException, HgRuntimeException; 84 public void serialize(DataSerializer out) throws HgIOException, HgRuntimeException;
105 public int serializeLength() { 107 public int serializeLength() {
106 return data == null ? 0 : data.length; 108 return data == null ? 0 : data.length;
107 } 109 }
108 } 110 }
109 111
110 public static class ByteArrayDataSerializer extends DataSerializer { 112 /**
113 * Serialize data to byte array
114 */
115 public static class ByteArraySerializer extends DataSerializer {
111 private final ByteArrayOutputStream out = new ByteArrayOutputStream(); 116 private final ByteArrayOutputStream out = new ByteArrayOutputStream();
112 117
113 @Override 118 @Override
114 public void write(byte[] data, int offset, int length) { 119 public void write(byte[] data, int offset, int length) {
115 out.write(data, offset, length); 120 out.write(data, offset, length);
117 122
118 public byte[] toByteArray() { 123 public byte[] toByteArray() {
119 return out.toByteArray(); 124 return out.toByteArray();
120 } 125 }
121 } 126 }
127
128 /**
129 * Bridge to the world of {@link java.io.OutputStream}.
130 * Caller instantiates the stream and is responsible to close it as appropriate,
131 * {@link #done() DataSerializer.done()} doesn't close the stream.
132 */
133 public static class OutputStreamSerializer extends DataSerializer {
134 private final OutputStream out;
135
136 public OutputStreamSerializer(OutputStream outputStream) {
137 out = outputStream;
138 }
139
140 @Override
141 public void write(byte[] data, int offset, int length) throws HgIOException {
142 try {
143 out.write(data, offset, length);
144 } catch (IOException ex) {
145 throw new HgIOException(ex.getMessage(), ex, null);
146 }
147 }
148 }
122 } 149 }