diff src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java @ 667:fba85bc1dfb8

Refactoring: move all encoding/decoding operations into single place, EncodingHelper
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 11 Jul 2013 17:54:08 +0200
parents 4e6179bde4fc
children
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java	Wed Jul 10 20:22:07 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java	Thu Jul 11 17:54:08 2013 +0200
@@ -17,7 +17,6 @@
 package org.tmatesoft.hg.internal;
 
 import java.io.ByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -31,7 +30,6 @@
 import org.tmatesoft.hg.core.HgIOException;
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.internal.DataSerializer.DataSource;
-import org.tmatesoft.hg.repo.HgInvalidStateException;
 import org.tmatesoft.hg.util.Path;
 
 /**
@@ -41,6 +39,7 @@
  */
 public class ChangelogEntryBuilder implements DataSource {
 
+	private final EncodingHelper encHelper;
 	private String user;
 	private List<Path> modifiedFiles;
 	private final Map<String, String> extrasMap = new LinkedHashMap<String, String>();
@@ -49,6 +48,10 @@
 	private Nodeid manifestRev;
 	private CharSequence comment;
 	
+	ChangelogEntryBuilder(EncodingHelper encodingHelper) {
+		encHelper = encodingHelper;
+	}
+	
 	public ChangelogEntryBuilder user(String username) {
 		user = username;
 		return this;
@@ -116,36 +119,32 @@
 	}
 
 	public byte[] build() {
-		try {
-			ByteArrayOutputStream out = new ByteArrayOutputStream();
-			final int LF = '\n';
-			CharSequence extras = buildExtras();
-			CharSequence files = buildFiles();
-			byte[] manifestRevision = manifestRev.toString().getBytes();
-			byte[] username = user().getBytes(EncodingHelper.getUTF8().name()); // XXX Java 1.5
-			out.write(manifestRevision, 0, manifestRevision.length);
-			out.write(LF);
-			out.write(username, 0, username.length);
-			out.write(LF);
-			final long csetDate = csetTime();
-			byte[] date = String.format("%d %d", csetDate, csetTimezone(csetDate)).getBytes();
-			out.write(date, 0, date.length);
-			if (extras.length() > 0) {
-				out.write(' ');
-				byte[] b = extras.toString().getBytes();
-				out.write(b, 0, b.length);
-			}
-			out.write(LF);
-			byte[] b = files.toString().getBytes();
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		final int LF = '\n';
+		CharSequence extras = buildExtras();
+		CharSequence files = buildFiles();
+		byte[] manifestRevision = manifestRev.toString().getBytes();
+		byte[] username = encHelper.userToChangeset(user());
+		out.write(manifestRevision, 0, manifestRevision.length);
+		out.write(LF);
+		out.write(username, 0, username.length);
+		out.write(LF);
+		final long csetDate = csetTime();
+		byte[] date = String.format("%d %d", csetDate, csetTimezone(csetDate)).getBytes();
+		out.write(date, 0, date.length);
+		if (extras.length() > 0) {
+			out.write(' ');
+			byte[] b = extras.toString().getBytes();
 			out.write(b, 0, b.length);
-			out.write(LF);
-			out.write(LF);
-			byte[] cmt = comment.toString().getBytes(EncodingHelper.getUTF8().name()); // XXX Java 1.5
-			out.write(cmt, 0, cmt.length);
-			return out.toByteArray();
-		} catch (UnsupportedEncodingException ex) {
-			throw new HgInvalidStateException(ex.getMessage()); // Can't happen, UTF8 is always there
 		}
+		out.write(LF);
+		byte[] b = encHelper.fileToChangeset(files);
+		out.write(b, 0, b.length);
+		out.write(LF);
+		out.write(LF);
+		byte[] cmt = encHelper.commentToChangeset(comment);
+		out.write(cmt, 0, cmt.length);
+		return out.toByteArray();
 	}
 
 	private CharSequence buildExtras() {