aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Borowitz2012-02-27 14:54:19 (EST)
committerShawn O. Pearce2012-02-29 22:09:23 (EST)
commit27cbdaf4975b01a23a95e94b6367318d3c7c4e0b (patch)
treebbcfb651716891b5f9b11d89d5bcfc7a5526e522
parent4a01f47e824aba1898ee7aa1f0677b8014210cfb (diff)
downloadjgit-27cbdaf4975b01a23a95e94b6367318d3c7c4e0b.zip
jgit-27cbdaf4975b01a23a95e94b6367318d3c7c4e0b.tar.gz
jgit-27cbdaf4975b01a23a95e94b6367318d3c7c4e0b.tar.bz2
Expose an OutputStream from ReceivePack for sending client messagesrefs/changes/70/5170/5
Callers may want to format and flush their own output, for example in a PreReceiveHook that creates its own TextProgressMonitor. The actual underlying msgOut can change over the lifetime of ReceivePack, so we implement a small wrapper. Change-Id: I57b6d6cad2542aaa93dcadc06cb3e933e81bcd3d
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java67
1 files changed, 55 insertions, 12 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
index 0bf84ef..af42dee 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java
@@ -157,6 +157,8 @@ public class ReceivePack {
private OutputStream msgOut;
+ private final MessageOutputWrapper msgOutWrapper = new MessageOutputWrapper();
+
private PacketLineIn pckIn;
private PacketLineOut pckOut;
@@ -246,6 +248,52 @@ public class ReceivePack {
}
}
+ /**
+ * Output stream that wraps the current {@link #msgOut}.
+ * <p>
+ * We don't want to expose {@link #msgOut} directly because it can change
+ * several times over the course of a session.
+ */
+ private class MessageOutputWrapper extends OutputStream {
+ @Override
+ public void write(int ch) {
+ if (msgOut != null) {
+ try {
+ msgOut.write(ch);
+ } catch (IOException e) {
+ // Ignore write failures.
+ }
+ }
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) {
+ if (msgOut != null) {
+ try {
+ msgOut.write(b, off, len);
+ } catch (IOException e) {
+ // Ignore write failures.
+ }
+ }
+ }
+
+ @Override
+ public void write(byte[] b) {
+ write(b, 0, b.length);
+ }
+
+ @Override
+ public void flush() {
+ if (msgOut != null) {
+ try {
+ msgOut.flush();
+ } catch (IOException e) {
+ // Ignore write failures.
+ }
+ }
+ }
+ }
+
/** @return the repository this receive completes into. */
public final Repository getRepository() {
return db;
@@ -542,12 +590,7 @@ public class ReceivePack {
advertiseError = new StringBuilder();
advertiseError.append(what).append('\n');
} else {
- try {
- if (msgOut != null)
- msgOut.write(Constants.encode("error: " + what + "\n"));
- } catch (IOException e) {
- // Ignore write failures.
- }
+ msgOutWrapper.write(Constants.encode("error: " + what + "\n"));
}
}
@@ -562,12 +605,12 @@ public class ReceivePack {
* string must not end with an LF, and must not contain an LF.
*/
public void sendMessage(final String what) {
- try {
- if (msgOut != null)
- msgOut.write(Constants.encode(what + "\n"));
- } catch (IOException e) {
- // Ignore write failures.
- }
+ msgOutWrapper.write(Constants.encode(what + "\n"));
+ }
+
+ /** @return an underlying stream for sending messages to the client. */
+ public OutputStream getMessageOutputStream() {
+ return msgOutWrapper;
}
/**