From f7e6eabd17ba20d0dbc0affaad5cd6c0f99a4401 Mon Sep 17 00:00:00 2001 From: Niels Ferguson Date: Wed, 3 Feb 2021 14:36:50 +0100 Subject: [PATCH] Fix incorrect usage of StringBuilder#append in TransactionAwareBufferedWriter Issue #3745 --- .../TransactionAwareBufferedWriter.java | 5 ++-- .../TransactionAwareBufferedWriterTests.java | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java index f48a2331d..97e834886 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * * @author Dave Syer * @author Michael Minella + * @author Niels Ferguson * */ public class TransactionAwareBufferedWriter extends Writer { @@ -244,6 +245,6 @@ public class TransactionAwareBufferedWriter extends Writer { } StringBuilder buffer = getCurrentBuffer(); - buffer.append(str, off, len); + buffer.append(str, off, off + len); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java index 2a588fd7b..638d6d68c 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2012 the original author or authors. + * Copyright 2006-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,7 @@ import static org.mockito.Mockito.when; * @author Dave Syer * @author Michael Minella * @author Will Schipp + * @author Niels Ferguson * */ public class TransactionAwareBufferedWriterTests { @@ -337,6 +338,27 @@ public class TransactionAwareBufferedWriterTests { } } + //BATCH-3745 + @Test + public void testWriteInTransactionWithOffset() throws IOException{ + ArgumentCaptor bb = ArgumentCaptor.forClass(ByteBuffer.class); + when(fileChannel.write(bb.capture())).thenReturn(3); + + new TransactionTemplate(transactionManager).execute((TransactionCallback) status -> { + + try { + writer.write("hamburger", 4, 3); + } catch (IOException e) { + throw new IllegalStateException("Unexpected IOException", e); + } + return null; + }); + + String s = getStringFromByteBuffer(bb.getValue()); + + assertEquals("urg", s); + } + private String getStringFromByteBuffer(ByteBuffer bb) { byte[] bytearr = new byte[bb.remaining()]; bb.get(bytearr);