diff 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
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/DataSerializer.java	Wed Jun 19 16:04:24 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/DataSerializer.java	Thu Jun 20 19:15:09 2013 +0200
@@ -17,6 +17,8 @@
 package org.tmatesoft.hg.internal;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
 
 import org.tmatesoft.hg.core.HgIOException;
 import org.tmatesoft.hg.repo.HgRuntimeException;
@@ -74,7 +76,7 @@
 	 * Denotes an entity that wants to/could be serialized
 	 */
 	@Experimental(reason="Work in progress")
-	interface DataSource {
+	public interface DataSource {
 		/**
 		 * Invoked once for a single write operation, 
 		 * although the source itself may get serialized several times
@@ -107,7 +109,10 @@
 		}
 	}
 	
-	public static class ByteArrayDataSerializer extends DataSerializer {
+	/**
+	 * Serialize data to byte array
+	 */
+	public static class ByteArraySerializer extends DataSerializer {
 		private final ByteArrayOutputStream out = new ByteArrayOutputStream();
 
 		@Override
@@ -119,4 +124,26 @@
 			return out.toByteArray();
 		}
 	}
+
+	/**
+	 * Bridge to the world of {@link java.io.OutputStream}.
+	 * Caller instantiates the stream and is responsible to close it as appropriate, 
+	 * {@link #done() DataSerializer.done()} doesn't close the stream. 
+	 */
+	public static class OutputStreamSerializer extends DataSerializer {
+		private final OutputStream out;
+
+		public OutputStreamSerializer(OutputStream outputStream) {
+			out = outputStream;
+		}
+
+		@Override
+		public void write(byte[] data, int offset, int length) throws HgIOException {
+			try {
+				out.write(data, offset, length);
+			} catch (IOException ex) {
+				throw new HgIOException(ex.getMessage(), ex, null);
+			}
+		}
+	}
 }