MessageWriter.java

  1. /*
  2.  * Copyright (C) 2009-2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.util.io;

  11. import static java.nio.charset.StandardCharsets.UTF_8;

  12. import java.io.ByteArrayOutputStream;
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. import java.io.OutputStreamWriter;
  16. import java.io.Writer;

  17. import org.eclipse.jgit.util.RawParseUtils;

  18. /**
  19.  * Combines messages from an OutputStream (hopefully in UTF-8) and a Writer.
  20.  * <p>
  21.  * This class is primarily meant for {@code BaseConnection} in contexts where a
  22.  * standard error stream from a command execution, as well as messages from a
  23.  * side-band channel, need to be combined together into a buffer to represent
  24.  * the complete set of messages from a remote repository.
  25.  * <p>
  26.  * Writes made to the writer are re-encoded as UTF-8 and interleaved into the
  27.  * buffer that {@link #getRawStream()} also writes to.
  28.  * <p>
  29.  * {@link #toString()} returns all written data, after converting it to a String
  30.  * under the assumption of UTF-8 encoding.
  31.  * <p>
  32.  * Internally {@link org.eclipse.jgit.util.RawParseUtils#decode(byte[])} is used
  33.  * by {@code toString()} tries to work out a reasonably correct character set
  34.  * for the raw data.
  35.  */
  36. public class MessageWriter extends Writer {
  37.     private final ByteArrayOutputStream buf;

  38.     private final OutputStreamWriter enc;

  39.     /**
  40.      * Create an empty writer.
  41.      */
  42.     public MessageWriter() {
  43.         buf = new ByteArrayOutputStream();
  44.         enc = new OutputStreamWriter(getRawStream(), UTF_8);
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public void write(char[] cbuf, int off, int len) throws IOException {
  49.         synchronized (buf) {
  50.             enc.write(cbuf, off, len);
  51.             enc.flush();
  52.         }
  53.     }

  54.     /**
  55.      * Get the underlying byte stream that character writes to this writer drop
  56.      * into.
  57.      *
  58.      * @return the underlying byte stream that character writes to this writer
  59.      *         drop into. Writes to this stream should should be in UTF-8.
  60.      */
  61.     public OutputStream getRawStream() {
  62.         return buf;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public void close() throws IOException {
  67.         // Do nothing, we are buffered with no resources.
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public void flush() throws IOException {
  72.         // Do nothing, we are buffered with no resources.
  73.     }

  74.     /** @return string version of all buffered data. */
  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public String toString() {
  78.         return RawParseUtils.decode(buf.toByteArray());
  79.     }
  80. }