From d31694057dc2102864081d235dd453cb4fd5d1ee Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Tue, 9 Aug 2011 05:00:09 +0000 Subject: [PATCH] replace StringBuffer with StringBuilder in TextPiece git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1155208 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hwpf/HWPFOldDocument.java | 4 ++-- .../org/apache/poi/hwpf/dev/HWPFLister.java | 2 +- .../poi/hwpf/extractor/Word6Extractor.java | 2 +- .../org/apache/poi/hwpf/model/TextPiece.java | 24 ++++++++++++------- .../apache/poi/hwpf/model/TextPieceTable.java | 2 +- .../poi/hwpf/model/TestTextPieceTable.java | 8 +++---- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java index 08c60959cf..182e634445 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFOldDocument.java @@ -74,7 +74,7 @@ public class HWPFOldDocument extends HWPFDocumentCore { tpt = cft.getTextPieceTable(); for(TextPiece tp : tpt.getTextPieces()) { - text.append( tp.getStringBuffer() ); + text.append( tp.getStringBuilder() ); } } else { // TODO Discover if these older documents can ever hold Unicode Strings? @@ -94,7 +94,7 @@ public class HWPFOldDocument extends HWPFDocumentCore { ); tpt.add(tp); - text.append(tp.getStringBuffer()); + text.append(tp.getStringBuilder()); } _text = tpt.getText(); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java index 10b8a2467d..44f9e93e53 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/HWPFLister.java @@ -562,7 +562,7 @@ public final class HWPFLister if ( withText ) { - System.out.println( "\t" + textPiece.getStringBuffer() ); + System.out.println( "\t" + textPiece.getStringBuilder() ); } } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/extractor/Word6Extractor.java b/src/scratchpad/src/org/apache/poi/hwpf/extractor/Word6Extractor.java index b40aa396b5..123f501781 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/extractor/Word6Extractor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/extractor/Word6Extractor.java @@ -84,7 +84,7 @@ public final class Word6Extractor extends POIOLE2TextExtractor { // Fall back to ripping out the text pieces ret = new String[doc.getTextTable().getTextPieces().size()]; for(int i=0; i _pd = pd; // Validate - int textLength = ((StringBuffer)_buf).length(); + int textLength = ((CharSequence)_buf).length(); if(end-start != textLength) { throw new IllegalStateException("Told we're for characters " + start + " -> " + end + ", but actually covers " + textLength + " characters!"); } @@ -78,9 +78,9 @@ public final class TextPiece extends PropertyNode } /** - * Create the StringBuffer from the text and unicode flag + * Create the StringBuilder from the text and unicode flag */ - private static StringBuffer buildInitSB(byte[] text, PieceDescriptor pd) { + private static StringBuilder buildInitSB(byte[] text, PieceDescriptor pd) { String str; try { if(pd.isUnicode()) { @@ -91,7 +91,7 @@ public final class TextPiece extends PropertyNode } catch(UnsupportedEncodingException e) { throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!"); } - return new StringBuffer(str); + return new StringBuilder(str); } /** @@ -107,15 +107,21 @@ public final class TextPiece extends PropertyNode return _pd; } + @Deprecated public StringBuffer getStringBuffer() { - return (StringBuffer)_buf; + return new StringBuffer(getStringBuilder()); + } + + public StringBuilder getStringBuilder() + { + return (StringBuilder)_buf; } public byte[] getRawBytes() { try { - return ((StringBuffer)_buf).toString().getBytes(_usesUnicode ? + return ((CharSequence)_buf).toString().getBytes(_usesUnicode ? "UTF-16LE" : "Cp1252"); } catch (UnsupportedEncodingException ignore) { throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!"); @@ -130,7 +136,7 @@ public final class TextPiece extends PropertyNode */ public String substring(int start, int end) { - StringBuffer buf = (StringBuffer)_buf; + StringBuilder buf = (StringBuilder)_buf; // Validate if(start < 0) { @@ -167,7 +173,7 @@ public final class TextPiece extends PropertyNode int bufStart = overlapStart - myStart; int bufEnd = overlapEnd - myStart; - ((StringBuffer)_buf).delete(bufStart, bufEnd); + ((StringBuilder)_buf).delete(bufStart, bufEnd); } // We need to invoke this even if text from this piece is not being @@ -197,7 +203,7 @@ public final class TextPiece extends PropertyNode if (limitsAreEqual(o)) { TextPiece tp = (TextPiece)o; - return getStringBuffer().toString().equals(tp.getStringBuffer().toString()) && + return getStringBuilder().toString().equals(tp.getStringBuilder().toString()) && tp._usesUnicode == _usesUnicode && _pd.equals(tp._pd); } return false; diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java index 453d0285a1..b3063aa5b4 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java @@ -264,7 +264,7 @@ public class TextPieceTable implements CharIndexTranslator StringBuilder docText = new StringBuilder(); for ( TextPiece textPiece : _textPieces ) { - String toAppend = textPiece.getStringBuffer().toString(); + String toAppend = textPiece.getStringBuilder().toString(); int toAppendLength = toAppend.length(); if ( toAppendLength != textPiece.getEnd() - textPiece.getStart() ) diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java index bef1fc32e8..b44a3ba16b 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java @@ -70,13 +70,13 @@ public final class TestTextPieceTable extends TestCase { // All ascii, so stored in one big lump assertEquals(1, tbl.getTextPieces().size()); - TextPiece tp = (TextPiece)tbl.getTextPieces().get(0); + TextPiece tp = tbl.getTextPieces().get(0); assertEquals(0, tp.getStart()); assertEquals(339, tp.getEnd()); assertEquals(339, tp.characterLength()); assertEquals(339, tp.bytesLength()); - assertTrue(tp.getStringBuffer().toString().startsWith("This is a sample word document")); + assertTrue(tp.getStringBuilder().toString().startsWith("This is a sample word document")); // Save and re-load @@ -84,13 +84,13 @@ public final class TestTextPieceTable extends TestCase { tbl = docB.getTextTable(); assertEquals(1, tbl.getTextPieces().size()); - tp = (TextPiece)tbl.getTextPieces().get(0); + tp = tbl.getTextPieces().get(0); assertEquals(0, tp.getStart()); assertEquals(339, tp.getEnd()); assertEquals(339, tp.characterLength()); assertEquals(339, tp.bytesLength()); - assertTrue(tp.getStringBuffer().toString().startsWith("This is a sample word document")); + assertTrue(tp.getStringBuilder().toString().startsWith("This is a sample word document")); } /** -- 2.39.5