Skip to content

Commit

Permalink
Fixes java-decompiler#17, Option for Line Endings
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed May 23, 2015
1 parent c27f934 commit 587ed2d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import jd.gui.api.model.Indexes
import jd.gui.util.decompiler.ClassFileSourcePrinter
import jd.gui.util.decompiler.ContainerLoader
import jd.gui.util.decompiler.GuiPreferences
import jd.gui.util.io.NewlineOutputStream
import org.fife.ui.rsyntaxtextarea.DocumentRange
import org.fife.ui.rsyntaxtextarea.SyntaxConstants

Expand Down Expand Up @@ -156,7 +157,7 @@ class ClassFilePage
}

void save(API api, OutputStream os) {
os.withWriter('UTF-8') {
new NewlineOutputStream(os).withWriter('UTF-8') {
it.write(textArea.text)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import jd.gui.util.decompiler.GuiPreferences;
import jd.gui.spi.SourceSaver;
import jd.gui.util.decompiler.PlainTextPrinter;
import jd.gui.util.io.NewlineOutputStream;

import java.io.*;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void save(API api, Controller controller, Listener listener, Path path, C
ps.print("\n */");
}

try (OutputStream os = Files.newOutputStream(path)) {
try (OutputStream os = new NewlineOutputStream(Files.newOutputStream(path))) {
baos.writeTo(os);
} catch (IOException ignore) {
}
Expand Down
54 changes: 54 additions & 0 deletions services/src/main/java/jd/gui/util/io/NewlineOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2008-2015 Emmanuel Dupuy
* This program is made available under the terms of the GPLv3 License.
*/

package jd.gui.util.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

public class NewlineOutputStream extends FilterOutputStream {
private static byte[] lineSeparator;

public NewlineOutputStream(OutputStream os) {
super(os);

if (lineSeparator == null) {
String s = System.getProperty("line.separator");

if ((s == null) || (s.length() <= 0))
s = "\n";

lineSeparator = s.getBytes(Charset.forName("UTF-8"));
}
}

public void write(int b) throws IOException {
if (b == '\n') {
out.write(lineSeparator);
} else {
out.write(b);
}
}

public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}

public void write(byte b[], int off, int len) throws IOException {
int i;

for (i=off; i<len; i++) {
if (b[i] == '\n') {
out.write(b, off, i-off);
out.write(lineSeparator);
off = i+1;
}
}

out.write(b, off, i-off);
}
}

0 comments on commit 587ed2d

Please sign in to comment.