Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10fd371

Browse files
marschallfmbenhassine
authored andcommitted
Improve TransactionAwareBufferedWriter
TransactionAwareBufferedWriter offers a number of optimization potentials. First it creates an unnecessary local, temporary char[] in write(char[], int, int). Second it does not overwrite any of the #write(String) methods leading to unnecessary intermediate copies. * avoid local, temporary char[] in #write(char[], int, int) * overwrite #write(String) methods to avoid copies Together these two changes should help to reduce allocation rate. Issue: #1166
1 parent fe51fde commit 10fd371

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
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-2020 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.
@@ -210,9 +210,7 @@ public void flush() throws IOException {
210210
public void write(char[] cbuf, int off, int len) throws IOException {
211211

212212
if (!transactionActive()) {
213-
char [] subArray = new char[len];
214-
System.arraycopy(cbuf, off, subArray, 0, len);
215-
byte[] bytes = new String(subArray).getBytes(encoding);
213+
byte[] bytes = new String(cbuf, off, len).getBytes(encoding);
216214
int length = bytes.length;
217215
ByteBuffer bb = ByteBuffer.wrap(bytes);
218216
int bytesWritten = channel.write(bb);
@@ -225,4 +223,27 @@ public void write(char[] cbuf, int off, int len) throws IOException {
225223
StringBuilder buffer = getCurrentBuffer();
226224
buffer.append(cbuf, off, len);
227225
}
226+
227+
/*
228+
* (non-Javadoc)
229+
*
230+
* @see java.io.Writer#write(String, int, int)
231+
*/
232+
@Override
233+
public void write(String str, int off, int len) throws IOException {
234+
235+
if (!transactionActive()) {
236+
byte[] bytes = str.substring(off, off + len).getBytes(encoding);
237+
int length = bytes.length;
238+
ByteBuffer bb = ByteBuffer.wrap(bytes);
239+
int bytesWritten = channel.write(bb);
240+
if(bytesWritten != length) {
241+
throw new IOException("Unable to write all data. Bytes to write: " + len + ". Bytes written: " + bytesWritten);
242+
}
243+
return;
244+
}
245+
246+
StringBuilder buffer = getCurrentBuffer();
247+
buffer.append(str, off, len);
248+
}
228249
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /