changeset 622:4e6179bde4fc

Update to comply with Java 1.5 target
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 20 May 2013 16:56:40 +0200
parents 99ad1e3a4e4d
children fedc54356091
files src/org/tmatesoft/hg/core/HgCloneCommand.java src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java src/org/tmatesoft/hg/internal/CommitFacility.java src/org/tmatesoft/hg/internal/FileSystemHelper.java src/org/tmatesoft/hg/internal/RepoInitializer.java src/org/tmatesoft/hg/internal/WorkingCopyContent.java src/org/tmatesoft/hg/repo/HgLookup.java test/org/tmatesoft/hg/test/RepoUtils.java
diffstat 8 files changed, 72 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgCloneCommand.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/core/HgCloneCommand.java	Mon May 20 16:56:40 2013 +0200
@@ -178,17 +178,12 @@
 			pathFactory = ctx.getPathFactory();
 		}
 
-		public void initEmptyRepository() throws IOException {
+		public void initEmptyRepository() throws HgIOException, HgRepositoryNotFoundException {
 			repoInit.initEmptyRepository(hgDir);
-			try {
-				assert (repoInit.getRequires() & FNCACHE) != 0;
-				fncacheFile = new FNCacheFile(Internals.getInstance(new HgLookup(ctx).detect(hgDir)));
-			} catch (HgRepositoryNotFoundException ex) {
-				// SHALL NOT HAPPEN provided we initialized empty repository successfully
-				// TODO perhaps, with WriteDownMate moving to a more appropriate location,
-				// we could instantiate HgRepository (or Internals) by other means, without exception?
-				throw new IOException("Can't access fncache for newly created repository", ex);
-			}
+			assert (repoInit.getRequires() & FNCACHE) != 0;
+			// XXX perhaps, with WriteDownMate moving to a more appropriate location,
+			// we could instantiate HgRepository (or Internals) by other means, without exception?
+			fncacheFile = new FNCacheFile(Internals.getInstance(new HgLookup(ctx).detect(hgDir)));
 		}
 
 		public void complete() throws IOException {
--- a/src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/ChangelogEntryBuilder.java	Mon May 20 16:56:40 2013 +0200
@@ -17,6 +17,7 @@
 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;
@@ -30,6 +31,7 @@
 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;
 
 /**
@@ -114,32 +116,36 @@
 	}
 
 	public byte[] build() {
-		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());
-		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();
+		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();
 			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 = files.toString().getBytes();
-		out.write(b, 0, b.length);
-		out.write(LF);
-		out.write(LF);
-		byte[] cmt = comment.toString().getBytes(EncodingHelper.getUTF8());
-		out.write(cmt, 0, cmt.length);
-		return out.toByteArray();
 	}
 
 	private CharSequence buildExtras() {
--- a/src/org/tmatesoft/hg/internal/CommitFacility.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/CommitFacility.java	Mon May 20 16:56:40 2013 +0200
@@ -196,7 +196,7 @@
 			FileOutputStream fos = null;
 			try {
 				fos = new FileOutputStream(branchFile);
-				fos.write(newBranchValue.getBytes(EncodingHelper.getUTF8()));
+				fos.write(newBranchValue.getBytes(EncodingHelper.getUTF8().name())); // XXX Java 1.5
 				fos.flush();
 				fos.close();
 				fos = null;
--- a/src/org/tmatesoft/hg/internal/FileSystemHelper.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/FileSystemHelper.java	Mon May 20 16:56:40 2013 +0200
@@ -63,7 +63,9 @@
 		try {
 			execHelper.exec(command);
 		} catch (InterruptedException ex) {
-			throw new IOException(ex);
+			IOException e = new IOException();
+			ex.initCause(ex); // XXX Java 1.5
+			throw e;
 		}
 	}
 	
@@ -77,7 +79,9 @@
 		try {
 			execHelper.exec(command);
 		} catch (InterruptedException ex) {
-			throw new IOException(ex);
+			IOException e = new IOException();
+			ex.initCause(ex); // XXX Java 1.5
+			throw e;
 		}
 	}
 
@@ -90,12 +94,14 @@
 		String result = null;
 		try {
 			result = execHelper.exec(command).toString().trim();
-			if (result.isEmpty()) {
+			if (result.length() == 0) { // XXX Java 1.5 isEmpty()
 				return defaultValue;
 			}
 			return Integer.parseInt(result, 8);
 		} catch (InterruptedException ex) {
-			throw new IOException(ex);
+			IOException e = new IOException();
+			ex.initCause(ex); // XXX Java 1.5
+			throw e;
 		} catch (NumberFormatException ex) {
 			ctx.getLog().dump(getClass(), Warn, ex, String.format("Bad value for access rights:%s", result));
 			return defaultValue;
--- a/src/org/tmatesoft/hg/internal/RepoInitializer.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/RepoInitializer.java	Mon May 20 16:56:40 2013 +0200
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.nio.charset.Charset;
 
+import org.tmatesoft.hg.core.HgIOException;
 import org.tmatesoft.hg.core.SessionContext;
 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
 import org.tmatesoft.hg.util.PathRewrite;
@@ -59,22 +60,27 @@
 		return requiresFlags;
 	}
 
-	public void initEmptyRepository(File repoDir) throws IOException {
+	public void initEmptyRepository(File repoDir) throws HgIOException {
 		repoDir.mkdirs();
-		FileOutputStream requiresFile = new FileOutputStream(new File(repoDir, "requires"));
-		StringBuilder sb = new StringBuilder(40);
-		sb.append("revlogv1\n");
-		if ((requiresFlags & STORE) != 0) {
-			sb.append("store\n");
+		final File requiresFile = new File(repoDir, "requires");
+		try {
+			FileOutputStream requiresStream = new FileOutputStream(requiresFile);
+			StringBuilder sb = new StringBuilder(40);
+			sb.append("revlogv1\n");
+			if ((requiresFlags & STORE) != 0) {
+				sb.append("store\n");
+			}
+			if ((requiresFlags & FNCACHE) != 0) {
+				sb.append("fncache\n");
+			}
+			if ((requiresFlags & DOTENCODE) != 0) {
+				sb.append("dotencode\n");
+			}
+			requiresStream.write(sb.toString().getBytes());
+			requiresStream.close();
+		} catch (IOException ex) {
+			throw new HgIOException("Failed to initialize empty repo", ex, requiresFile);
 		}
-		if ((requiresFlags & FNCACHE) != 0) {
-			sb.append("fncache\n");
-		}
-		if ((requiresFlags & DOTENCODE) != 0) {
-			sb.append("dotencode\n");
-		}
-		requiresFile.write(sb.toString().getBytes());
-		requiresFile.close();
 		new File(repoDir, "store").mkdir(); // with that, hg verify says ok.
 	}
 
--- a/src/org/tmatesoft/hg/internal/WorkingCopyContent.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/WorkingCopyContent.java	Mon May 20 16:56:40 2013 +0200
@@ -64,7 +64,9 @@
 						return rv;
 					} catch (HgIOException ex) {
 						failure[0] = ex;
-						throw new IOException(ex);
+						IOException e = new IOException();
+						ex.initCause(ex); // XXX Java 1.5
+						throw e;
 					}
 				}
 			});
--- a/src/org/tmatesoft/hg/repo/HgLookup.java	Sat May 18 22:23:57 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgLookup.java	Mon May 20 16:56:40 2013 +0200
@@ -118,7 +118,7 @@
 			String server = null;
 			if (hgRepo != null && !hgRepo.isInvalid()) {
 				PathsSection ps = hgRepo.getConfiguration().getPaths();
-				server = key == null || key.trim().isEmpty() ? ps.getDefault() : ps.getString(key, null);
+				server = key == null || key.trim().length() == 0 ? ps.getDefault() : ps.getString(key, null); // XXX Java 1.5 isEmpty() 
 			} else if (key == null || key.trim().length() == 0) {
 				throw new HgBadArgumentException("Can't look up empty key in a global configuration", null);
 			}
--- a/test/org/tmatesoft/hg/test/RepoUtils.java	Sat May 18 22:23:57 2013 +0200
+++ b/test/org/tmatesoft/hg/test/RepoUtils.java	Mon May 20 16:56:40 2013 +0200
@@ -26,6 +26,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 
+import org.tmatesoft.hg.core.HgIOException;
 import org.tmatesoft.hg.internal.RepoInitializer;
 import org.tmatesoft.hg.repo.HgRepository;
 
@@ -36,7 +37,7 @@
  */
 public class RepoUtils {
 
-	static File initEmptyTempRepo(String dirName) throws IOException {
+	static File initEmptyTempRepo(String dirName) throws IOException, HgIOException {
 		File dest = createEmptyDir(dirName);
 		RepoInitializer ri = new RepoInitializer();
 		ri.setRequires(STORE | FNCACHE | DOTENCODE);