BATCH-1799: Updated to handle encoding correctly

This commit is contained in:
Michael Minella
2012-11-16 13:11:27 -06:00
parent 13fbcf992a
commit 4ec551e0ff
3 changed files with 44 additions and 25 deletions

View File

@@ -579,23 +579,27 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
private Writer getBufferedWriter(FileChannel fileChannel, String encoding) {
try {
final FileChannel channel = fileChannel;
Writer writer = new BufferedWriter(Channels.newWriter(fileChannel, encoding)) {
@Override
public void flush() throws IOException {
super.flush();
if (forceSync) {
channel.force(false);
}
}
};
if (transactional) {
return new TransactionAwareBufferedWriter(channel, new Runnable() {
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() {
public void run() {
closeStream();
}
});
writer.setEncoding(encoding);
return writer;
}
else {
Writer writer = new BufferedWriter(Channels.newWriter(fileChannel, encoding)) {
@Override
public void flush() throws IOException {
super.flush();
if (forceSync) {
channel.force(false);
}
}
};
return new BufferedWriter(writer);
}
}

View File

@@ -409,23 +409,26 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
try {
final FileChannel channel = fileChannel;
Writer writer = new BufferedWriter(new OutputStreamWriter(os, encoding)) {
@Override
public void flush() throws IOException {
super.flush();
if (forceSync) {
channel.force(false);
}
}
};
if (transactional) {
bufferedWriter = new TransactionAwareBufferedWriter(channel, new Runnable() {
TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() {
public void run() {
closeStream();
}
});
writer.setEncoding(encoding);
bufferedWriter = writer;
}
else {
Writer writer = new BufferedWriter(new OutputStreamWriter(os, encoding)) {
@Override
public void flush() throws IOException {
super.flush();
if (forceSync) {
channel.force(false);
}
}
};
bufferedWriter = writer;
}
delegateEventWriter = createXmlEventWriter(outputFactory, bufferedWriter);

View File

@@ -47,7 +47,12 @@ public class TransactionAwareBufferedWriter extends Writer {
private FileChannel channel;
private final Runnable closeCallback;
// default encoding for writing to output files - set to UTF-8.
private static final String DEFAULT_CHARSET = "UTF-8";
private String encoding = DEFAULT_CHARSET;
/**
* Create a new instance with the underlying file channel provided, and a callback
* to execute on close. The callback should clean up related resources like
@@ -64,6 +69,10 @@ public class TransactionAwareBufferedWriter extends Writer {
this.closeKey = CLOSE_KEY_PREFIX + "." + hashCode();
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* @return
*/
@@ -95,8 +104,9 @@ public class TransactionAwareBufferedWriter extends Writer {
StringBuffer buffer = (StringBuffer) TransactionSynchronizationManager.getResource(bufferKey);
if (buffer != null) {
String string = buffer.toString();
int bufferLength = string.length();
ByteBuffer bb = ByteBuffer.wrap(string.getBytes());
byte[] bytes = string.getBytes(encoding);
int bufferLength = bytes.length;
ByteBuffer bb = ByteBuffer.wrap(bytes);
int bytesWritten = channel.write(bb);
if(bytesWritten != bufferLength) {
throw new IOException("All bytes to be written were not successfully written");
@@ -181,9 +191,11 @@ public class TransactionAwareBufferedWriter extends Writer {
public void write(char[] cbuf, int off, int len) throws IOException {
if (!transactionActive()) {
ByteBuffer bb = ByteBuffer.wrap(new String(Arrays.copyOfRange(cbuf, off, off + len)).getBytes());
byte[] bytes = new String(Arrays.copyOfRange(cbuf, off, off + len)).getBytes(encoding);
int length = bytes.length;
ByteBuffer bb = ByteBuffer.wrap(bytes);
int bytesWritten = channel.write(bb);
if(bytesWritten != len) {
if(bytesWritten != length) {
throw new IOException("Unable to write all data. Bytes to write: " + len + ". Bytes written: " + bytesWritten);
}
return;