Skip to content

Commit

Permalink
Improved encoding management for CDO text resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Sep 4, 2023
1 parent e9aa971 commit 8f07f39
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void close() throws IOException {
if (node instanceof CDOBinaryResource) {
((CDOBinaryResource) node).setContents(new CDOBlob(new ByteArrayInputStream(buffer.toByteArray())));
} else if (node instanceof CDOTextResource) {
((CDOTextResource) node).setContents(new CDOClob(new StringReader(buffer.toString("UTF-8"))));
final CDOTextResource textResource = (CDOTextResource) node;
final String encoding = getEncoding(textResource);
textResource.setContents(new CDOClob(new StringReader(buffer.toString(encoding))));
} else {
throw new IllegalStateException("CDOResourceNode type not supported.");
}
Expand Down Expand Up @@ -108,14 +110,33 @@ public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOExcept
if (node instanceof CDOBinaryResource) {
res = ((CDOBinaryResource) node).getContents().getContents();
} else if (node instanceof CDOTextResource) {
res = new ReaderInputStream(((CDOTextResource) node).getContents().getContents(), "UTF-8");
final CDOTextResource textResource = (CDOTextResource) node;
final String encoding = getEncoding(textResource);
res = new ReaderInputStream(textResource.getContents().getContents(), encoding);
} else {
res = super.createInputStream(uri, options);
}

return res;
}

/**
* Gets the encoding of the given {@link CDOTextResource} and fallback to UTF-8.
*
* @param textResource
* the {@link CDOTextResource}
* @return the encoding of the given {@link CDOTextResource} and fallback to UTF-8
*/
private static String getEncoding(final CDOTextResource textResource) {
final String encoding;
if (textResource.getEncoding() != null) {
encoding = textResource.getEncoding();
} else {
encoding = "UTF-8";
}
return encoding;
}

@Override
public OutputStream createOutputStream(URI uri, Map<?, ?> options) throws IOException {
final OutputStream res;
Expand Down

0 comments on commit 8f07f39

Please sign in to comment.