editorconfig-netbeans
editorconfig-netbeans copied to clipboard
Test that files with "charset" declaration are saved with their file marks
See https://github.com/welovecoding/editorconfig-netbeans/issues/21
"\uFEFF" will be FF FE which indicates UTF-16LE BOM.
Another option would be:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(string.length() * 2 + 2);
byteArrayOutputStream.write(new byte[]{(byte)0xFF,(byte)0xFE});
byteArrayOutputStream.write(string.getBytes("UTF-16LE"));
return byteArrayOutputStream.toByteArray();
Helpful snippets
DataEditorSupport des = dataObject.getLookup().lookup(DataEditorSupport.class);
-> saveFromKitToStream( StyledDocument myDoc, OutputStream os )
public static EditorKit getEditorKit(String mimePath) {
Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimePath));
EditorKit kit = lookup.lookup(EditorKit.class);
if (kit == null) {
// Try 'text/plain'
lookup = MimeLookup.getLookup(MimePath.parse("text/plain"));
kit = lookup.lookup(EditorKit.class);
}
// Don't use the prototype instance straightaway
return kit != null ? (EditorKit) kit.clone() : new PlainEditorKit();
}
Charset c = charsets.get(this.getDataObject());
if (c == null) {
c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
}
FilterOutputStream fos = new FilterOutputStream(stream) {
@Override
public void close() throws IOException {
flush();
}
};
Writer w = new OutputStreamWriter (fos, c);
try {
kit.write(w, doc, 0, doc.getLength());
} finally {
w.close();
}
private void saveFromKitToStream(FileObject fo, StyledDocument doc, Charset c, OutputStream stream) {
EditorKit kit = NetBeansFileUtil.getEditorKit(fo);
FilterOutputStream fos = new FilterOutputStream(stream) {
@Override
public void close() throws IOException {
flush();
}
};
Writer w = new OutputStreamWriter(fos, c);
try {
kit.write(w, doc, 0, doc.getLength());
} catch (IOException | BadLocationException ex) {
Exceptions.printStackTrace(ex);
} finally {
try {
w.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
}
Approach to set charset for EditorKit reader:
"Another way the character set can be specified is in the document itself. This requires reading the document prior to determining the character set that is desired. To handle this, it is expected that the EditorKit.read operation throw a ChangedCharSetException which will be caught. The read is then restarted with a new Reader that uses the character set specified in the ChangedCharSetException (which is an IOException)"
Source: https://docs.oracle.com/javase/7/docs/api/javax/swing/JEditorPane.html
Code samples: http://www.programcreek.com/java-api-examples/index.php?api=javax.swing.text.ChangedCharSetException
BOMs in bytes: http://stackoverflow.com/a/1888284/451634
EOLs can be detected in Swing with: textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty );
Interesting: http://commons.apache.org/proper/commons-net/jacoco/org.apache.commons.net.io/CRLFLineReader.java.html