AutoCRLFOutputStream.java

  1. /*
  2.  * Copyright (C) 2011, 2013 Robin Rosenberg
  3.  * and other copyright owners as documented in the project's IP log.
  4.  *
  5.  * This program and the accompanying materials are made available
  6.  * under the terms of the Eclipse Distribution License v1.0 which
  7.  * accompanies this distribution, is reproduced below, and is
  8.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or
  13.  * without modification, are permitted provided that the following
  14.  * conditions are met:
  15.  *
  16.  * - Redistributions of source code must retain the above copyright
  17.  *   notice, this list of conditions and the following disclaimer.
  18.  *
  19.  * - Redistributions in binary form must reproduce the above
  20.  *   copyright notice, this list of conditions and the following
  21.  *   disclaimer in the documentation and/or other materials provided
  22.  *   with the distribution.
  23.  *
  24.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  25.  *   names of its contributors may be used to endorse or promote
  26.  *   products derived from this software without specific prior
  27.  *   written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  */

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

  44. import java.io.IOException;
  45. import java.io.OutputStream;

  46. import org.eclipse.jgit.diff.RawText;

  47. /**
  48.  * An OutputStream that expands LF to CRLF.
  49.  *
  50.  * Existing CRLF are not expanded to CRCRLF, but retained as is.
  51.  *
  52.  * A binary check on the first 8000 bytes is performed and in case of binary
  53.  * files, canonicalization is turned off (for the complete file).
  54.  */
  55. public class AutoCRLFOutputStream extends OutputStream {

  56.     static final int BUFFER_SIZE = 8000;

  57.     private final OutputStream out;

  58.     private int buf = -1;

  59.     private byte[] binbuf = new byte[BUFFER_SIZE];

  60.     private byte[] onebytebuf = new byte[1];

  61.     private int binbufcnt = 0;

  62.     private boolean detectBinary;

  63.     private boolean isBinary;

  64.     /**
  65.      * <p>Constructor for AutoCRLFOutputStream.</p>
  66.      *
  67.      * @param out a {@link java.io.OutputStream} object.
  68.      */
  69.     public AutoCRLFOutputStream(OutputStream out) {
  70.         this(out, true);
  71.     }

  72.     /**
  73.      * <p>Constructor for AutoCRLFOutputStream.</p>
  74.      *
  75.      * @param out a {@link java.io.OutputStream} object.
  76.      * @param detectBinary
  77.      *            whether binaries should be detected
  78.      * @since 4.3
  79.      */
  80.     public AutoCRLFOutputStream(OutputStream out, boolean detectBinary) {
  81.         this.out = out;
  82.         this.detectBinary = detectBinary;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public void write(int b) throws IOException {
  87.         onebytebuf[0] = (byte) b;
  88.         write(onebytebuf, 0, 1);
  89.     }

  90.     /** {@inheritDoc} */
  91.     @Override
  92.     public void write(byte[] b) throws IOException {
  93.         int overflow = buffer(b, 0, b.length);
  94.         if (overflow > 0)
  95.             write(b, b.length - overflow, overflow);
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public void write(byte[] b, int startOff, int startLen)
  100.             throws IOException {
  101.         final int overflow = buffer(b, startOff, startLen);
  102.         if (overflow < 0)
  103.             return;
  104.         final int off = startOff + startLen - overflow;
  105.         final int len = overflow;
  106.         if (len == 0)
  107.             return;
  108.         int lastw = off;
  109.         if (isBinary) {
  110.             out.write(b, off, len);
  111.             return;
  112.         }
  113.         for (int i = off; i < off + len; ++i) {
  114.             final byte c = b[i];
  115.             if (c == '\r') {
  116.                 buf = '\r';
  117.             } else if (c == '\n') {
  118.                 if (buf != '\r') {
  119.                     if (lastw < i) {
  120.                         out.write(b, lastw, i - lastw);
  121.                     }
  122.                     out.write('\r');
  123.                     lastw = i;
  124.                 }
  125.                 buf = -1;
  126.             } else {
  127.                 buf = -1;
  128.             }
  129.         }
  130.         if (lastw < off + len) {
  131.             out.write(b, lastw, off + len - lastw);
  132.         }
  133.         if (b[off + len - 1] == '\r')
  134.             buf = '\r';
  135.     }

  136.     private int buffer(byte[] b, int off, int len) throws IOException {
  137.         if (binbufcnt > binbuf.length)
  138.             return len;
  139.         int copy = Math.min(binbuf.length - binbufcnt, len);
  140.         System.arraycopy(b, off, binbuf, binbufcnt, copy);
  141.         binbufcnt += copy;
  142.         int remaining = len - copy;
  143.         if (remaining > 0)
  144.             decideMode();
  145.         return remaining;
  146.     }

  147.     private void decideMode() throws IOException {
  148.         if (detectBinary) {
  149.             isBinary = RawText.isBinary(binbuf, binbufcnt);
  150.             detectBinary = false;
  151.         }
  152.         int cachedLen = binbufcnt;
  153.         binbufcnt = binbuf.length + 1; // full!
  154.         write(binbuf, 0, cachedLen);
  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     public void flush() throws IOException {
  159.         if (binbufcnt <= binbuf.length)
  160.             decideMode();
  161.         buf = -1;
  162.         out.flush();
  163.     }

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     public void close() throws IOException {
  167.         flush();
  168.         out.close();
  169.     }
  170. }