From c0c4c6f09ac1d601a5d9fe9855e36e4b96b10335 Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Sun, 15 Feb 2015 20:31:29 +0100 Subject: ArchiveCommand: Allow to pass options to underlying stream Current ArchiveCommand design doesn't allow to pass in options to underlying stream implementations. To overcome this, client has to implement custom format implementation (it cannot be derived from the existing one, because the classes are marked as final), and set the options using ThreadLocal, before the method ArchiveOutputStream createArchiveOutputStream(OutputStream s) is get called. This change extends the ArchiveCommand.Format by allowing to pass option map during creation of ArchiveOutputStream. ArchiveCommand is extended correspondingly. That way client can easily pass options to the underlying streams: Map level = ImmutableMap. of( "level", new Integer(9)); new ArchiveCommand(repo) .setFormat("zip") .setFormatOptions(level) .setTree(tree) .setPaths(paths) .setPrefix(prefix) .setOutputStream(sidebandOut) .call(); Change-Id: I1d92a1e5249117487da39d19c7593e4b812ad97a Signed-off-by: David Ostrovsky --- .../src/org/eclipse/jgit/api/ArchiveCommand.java | 34 +++++++++++++++++++++- .../src/org/eclipse/jgit/util/StringUtils.java | 25 ++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'org.eclipse.jgit') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java index 616d5b436b..f52a934774 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ArchiveCommand.java @@ -48,7 +48,9 @@ import java.io.OutputStream; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -135,6 +137,25 @@ public class ArchiveCommand extends GitCommand { */ T createArchiveOutputStream(OutputStream s) throws IOException; + /** + * Start a new archive. Entries can be included in the archive using the + * putEntry method, and then the archive should be closed using its + * close method. In addition options can be applied to the underlying + * stream. E.g. compression level. + * + * @param s + * underlying output stream to which to write the archive. + * @param o + * options to apply to the underlying output stream. Keys are + * option names and values are option values. + * @return new archive object for use in putEntry + * @throws IOException + * thrown by the underlying output stream for I/O errors + * @since 4.0 + */ + T createArchiveOutputStream(OutputStream s, Map o) + throws IOException; + /** * Write an entry to an archive. * @@ -328,6 +349,7 @@ public class ArchiveCommand extends GitCommand { private ObjectId tree; private String prefix; private String format; + private Map formatOptions = new HashMap<>(); private List paths = new ArrayList(); /** Filename suffix, for automatically choosing a format. */ @@ -345,7 +367,7 @@ public class ArchiveCommand extends GitCommand { final String pfx = prefix == null ? "" : prefix; //$NON-NLS-1$ final TreeWalk walk = new TreeWalk(repo); try { - final T outa = fmt.createArchiveOutputStream(out); + final T outa = fmt.createArchiveOutputStream(out, formatOptions); try { final MutableObjectId idBuf = new MutableObjectId(); final ObjectReader reader = walk.getObjectReader(); @@ -471,6 +493,16 @@ public class ArchiveCommand extends GitCommand { return this; } + /** + * @param options + * archive format options (e.g., level=9 for zip compression). + * @return this + */ + public ArchiveCommand setFormatOptions(Map options) { + this.formatOptions = options; + return this; + } + /** * Set an optional parameter path. without an optional path parameter, all * files and subdirectories of the current working directory are included in diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java index 40b89fba9b..1fd5089300 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/StringUtils.java @@ -96,6 +96,31 @@ public final class StringUtils { return r.toString(); } + + /** + * Borrowed from commons-lang StringUtils.capitalize() method. + * + *

+ * Capitalizes a String changing the first letter to title case as per + * {@link Character#toTitleCase(char)}. No other letters are changed. + *

+ * + * A null input String returns null.

+ * + * @param str + * the String to capitalize, may be null + * @return the capitalized String, null if null String input + */ + public static String capitalize(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + return new StringBuffer(strLen) + .append(Character.toTitleCase(str.charAt(0))) + .append(str.substring(1)).toString(); + } + /** * Test if two strings are equal, ignoring case. *

-- cgit v1.2.3