diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java index f05da1c73c..9fb316f28d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java @@ -1,48 +1,16 @@ /* - * Copyright (C) 2011, 2013 Robin Rosenberg - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, 2013 Robin Rosenberg and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.util.io; +import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -50,30 +18,51 @@ import org.eclipse.jgit.diff.RawText; /** * An OutputStream that expands LF to CRLF. - * <p> + * * Existing CRLF are not expanded to CRCRLF, but retained as is. + * + * A binary check on the first {@link RawText#getBufferSize()} bytes is + * performed and in case of binary files, canonicalization is turned off (for + * the complete file). */ public class AutoCRLFOutputStream extends OutputStream { - static final int BUFFER_SIZE = 8000; - private final OutputStream out; private int buf = -1; - private byte[] binbuf = new byte[BUFFER_SIZE]; + private byte[] binbuf = new byte[RawText.getBufferSize()]; private byte[] onebytebuf = new byte[1]; private int binbufcnt = 0; + private boolean detectBinary; + private boolean isBinary; /** - * @param out + * <p>Constructor for AutoCRLFOutputStream.</p> + * + * @param out a {@link java.io.OutputStream} object. */ public AutoCRLFOutputStream(OutputStream out) { - this.out = out; + this(out, true); + } + + /** + * <p>Constructor for AutoCRLFOutputStream.</p> + * + * @param out a {@link java.io.OutputStream} object. + * @param detectBinary + * whether binaries should be detected + * @since 4.3 + */ + public AutoCRLFOutputStream(OutputStream out, boolean detectBinary) { + // avoid to write single lines directly to FileOutputStream: + this.out = out instanceof BufferedOutputStream ? out + : new BufferedOutputStream(out); + this.detectBinary = detectBinary; } @Override @@ -90,7 +79,7 @@ public class AutoCRLFOutputStream extends OutputStream { } @Override - public void write(byte[] b, final int startOff, final int startLen) + public void write(byte[] b, int startOff, int startLen) throws IOException { final int overflow = buffer(b, startOff, startLen); if (overflow < 0) @@ -106,9 +95,11 @@ public class AutoCRLFOutputStream extends OutputStream { } for (int i = off; i < off + len; ++i) { final byte c = b[i]; - if (c == '\r') { + switch (c) { + case '\r': buf = '\r'; - } else if (c == '\n') { + break; + case '\n': if (buf != '\r') { if (lastw < i) { out.write(b, lastw, i - lastw); @@ -117,8 +108,10 @@ public class AutoCRLFOutputStream extends OutputStream { lastw = i; } buf = -1; - } else { + break; + default: buf = -1; + break; } } if (lastw < off + len) { @@ -129,19 +122,27 @@ public class AutoCRLFOutputStream extends OutputStream { } private int buffer(byte[] b, int off, int len) throws IOException { - if (binbufcnt > binbuf.length) + if (binbufcnt > binbuf.length) { return len; + } int copy = Math.min(binbuf.length - binbufcnt, len); System.arraycopy(b, off, binbuf, binbufcnt, copy); binbufcnt += copy; int remaining = len - copy; - if (remaining > 0) - decideMode(); + if (remaining > 0) { + decideMode(false); + } return remaining; } - private void decideMode() throws IOException { - isBinary = RawText.isBinary(binbuf, binbufcnt); + private void decideMode(boolean complete) throws IOException { + if (detectBinary) { + isBinary = RawText.isBinary(binbuf, binbufcnt, complete); + if (!isBinary) { + isBinary = RawText.isCrLfText(binbuf, binbufcnt, complete); + } + detectBinary = false; + } int cachedLen = binbufcnt; binbufcnt = binbuf.length + 1; // full! write(binbuf, 0, cachedLen); @@ -149,8 +150,9 @@ public class AutoCRLFOutputStream extends OutputStream { @Override public void flush() throws IOException { - if (binbufcnt <= binbuf.length) - decideMode(); + if (binbufcnt <= binbuf.length) { + decideMode(true); + } buf = -1; out.flush(); } |