How to write to a StyledDocument with a specific charset?

356 views Asked by At

for a NetBeans plugin I want to change the content of a file (which is opened in the NetBeans editor) with a specific String and a specific charset. In order to achieve that, I open the file (a DataObject) with an EditorCookie and then I change the content by inserting a different string to the StyledDocument of my data object.

However, I have a feeling that the file is always saved as UTF-8. Even if I write a file mark in the file. Am I doing something wrong?

This is my code:

...

EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class);
String utf16be = new String("\uFEFFHello World!".getBytes(StandardCharsets.UTF_16BE));

NbDocument.runAtomic(cookie.getDocument(), () -> {
  try {
    StyledDocument document = cookie.openDocument();
    document.remove(0, document.getLength());
    document.insertString(0, utf16be, null);
    cookie.saveDocument();
  } catch (BadLocationException | IOException ex) {
    Exceptions.printStackTrace(ex);
  }
});

I have also tried this approach which doesn't work too:

... 

EditorCookie cookie = dataObject.getLookup().lookup(EditorCookie.class); 

NbDocument.runAtomic(cookie.getDocument(), () -> {
  try {
    StyledDocument doc = cookie.openDocument();

    String utf16be = "\uFEFFHello World!";
    InputStream is = new ByteArrayInputStream(utf16be.getBytes(StandardCharsets.UTF_16BE));

    FileObject fileObject = dataObject.getPrimaryFile();
    String mimePath = fileObject.getMIMEType();
    Lookup lookup = MimeLookup.getLookup(MimePath.parse(mimePath));
    EditorKit kit = lookup.lookup(EditorKit.class);

    try {
      kit.read(is, doc, doc.getLength());
    } catch (IOException | BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    } finally {
      is.close();
    }

    cookie.saveDocument();
  } catch (Exception ex) {
    Exceptions.printStackTrace(ex);
  }
});
1

There are 1 answers

6
fge On

Your problem is probably here:

String utf16be = new String("\uFEFFHello World!".getBytes(StandardCharsets.UTF_16BE));

This won't do what you think it does. This will convert your string to a byte array using the UTF-16 little endian encoding and then create a String from these bytes using the JRE's default encoding.

So, here's the catch:

A String has no encoding.

The fact that in Java this is a sequence of chars does not matter. Substitute 'char' for 'carrier pigeons', the net effect will be the same.

If you want to write a String to a byte stream with a given encoding, you need to specify the encoding you need on the Writer object you create. Similarly, if you want to read a byte stream into a String using a given encoding, it is the Reader which you need to configure to use the encoding you want.

But your StyledDocument object's method name is .insertString(); You should .insertString() your String object as is; don't transform it the way you do, since this is misguided, as explained above.