Skip to content

Commit f7e6eab

Browse files
nfjcorefmbenhassine
authored andcommitted
Fix incorrect usage of StringBuilder#append in TransactionAwareBufferedWriter
Issue #3745
1 parent e176058 commit f7e6eab

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @author Dave Syer
3535
* @author Michael Minella
36+
* @author Niels Ferguson
3637
*
3738
*/
3839
public class TransactionAwareBufferedWriter extends Writer {
@@ -244,6 +245,6 @@ public void write(String str, int off, int len) throws IOException {
244245
}
245246

246247
StringBuilder buffer = getCurrentBuffer();
247-
buffer.append(str, off, len);
248+
buffer.append(str, off, off + len);
248249
}
249250
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2012 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
* @author Dave Syer
4141
* @author Michael Minella
4242
* @author Will Schipp
43+
* @author Niels Ferguson
4344
*
4445
*/
4546
public class TransactionAwareBufferedWriterTests {
@@ -337,6 +338,27 @@ public void testResourceKeyCollision() throws Exception {
337338
}
338339
}
339340

341+
//BATCH-3745
342+
@Test
343+
public void testWriteInTransactionWithOffset() throws IOException{
344+
ArgumentCaptor<ByteBuffer> bb = ArgumentCaptor.forClass(ByteBuffer.class);
345+
when(fileChannel.write(bb.capture())).thenReturn(3);
346+
347+
new TransactionTemplate(transactionManager).execute((TransactionCallback<Void>) status -> {
348+
349+
try {
350+
writer.write("hamburger", 4, 3);
351+
} catch (IOException e) {
352+
throw new IllegalStateException("Unexpected IOException", e);
353+
}
354+
return null;
355+
});
356+
357+
String s = getStringFromByteBuffer(bb.getValue());
358+
359+
assertEquals("urg", s);
360+
}
361+
340362
private String getStringFromByteBuffer(ByteBuffer bb) {
341363
byte[] bytearr = new byte[bb.remaining()];
342364
bb.get(bytearr);

0 commit comments

Comments
 (0)