replace StringBuffer with StringBuilder in TextPiece

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1155208 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-08-09 05:00:09 +00:00
parent d7a0a6ee85
commit 7a333045e5
6 changed files with 24 additions and 18 deletions

View File

@ -74,7 +74,7 @@ public class HWPFOldDocument extends HWPFDocumentCore {
tpt = cft.getTextPieceTable(); tpt = cft.getTextPieceTable();
for(TextPiece tp : tpt.getTextPieces()) { for(TextPiece tp : tpt.getTextPieces()) {
text.append( tp.getStringBuffer() ); text.append( tp.getStringBuilder() );
} }
} else { } else {
// TODO Discover if these older documents can ever hold Unicode Strings? // TODO Discover if these older documents can ever hold Unicode Strings?
@ -94,7 +94,7 @@ public class HWPFOldDocument extends HWPFDocumentCore {
); );
tpt.add(tp); tpt.add(tp);
text.append(tp.getStringBuffer()); text.append(tp.getStringBuilder());
} }
_text = tpt.getText(); _text = tpt.getText();

View File

@ -562,7 +562,7 @@ public final class HWPFLister
if ( withText ) if ( withText )
{ {
System.out.println( "\t" + textPiece.getStringBuffer() ); System.out.println( "\t" + textPiece.getStringBuilder() );
} }
} }
} }

View File

@ -84,7 +84,7 @@ public final class Word6Extractor extends POIOLE2TextExtractor {
// Fall back to ripping out the text pieces // Fall back to ripping out the text pieces
ret = new String[doc.getTextTable().getTextPieces().size()]; ret = new String[doc.getTextTable().getTextPieces().size()];
for(int i=0; i<ret.length; i++) { for(int i=0; i<ret.length; i++) {
ret[i] = doc.getTextTable().getTextPieces().get(i).getStringBuffer().toString(); ret[i] = doc.getTextTable().getTextPieces().get(i).getStringBuilder().toString();
// Fix the line endings // Fix the line endings
ret[i].replaceAll("\r", "\ufffe"); ret[i].replaceAll("\r", "\ufffe");

View File

@ -68,7 +68,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
_pd = pd; _pd = pd;
// Validate // Validate
int textLength = ((StringBuffer)_buf).length(); int textLength = ((CharSequence)_buf).length();
if(end-start != textLength) { if(end-start != textLength) {
throw new IllegalStateException("Told we're for characters " + start + " -> " + end + ", but actually covers " + textLength + " characters!"); 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<TextPiece>
} }
/** /**
* 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; String str;
try { try {
if(pd.isUnicode()) { if(pd.isUnicode()) {
@ -91,7 +91,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
} catch(UnsupportedEncodingException e) { } catch(UnsupportedEncodingException e) {
throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!"); 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<TextPiece>
return _pd; return _pd;
} }
@Deprecated
public StringBuffer getStringBuffer() public StringBuffer getStringBuffer()
{ {
return (StringBuffer)_buf; return new StringBuffer(getStringBuilder());
}
public StringBuilder getStringBuilder()
{
return (StringBuilder)_buf;
} }
public byte[] getRawBytes() public byte[] getRawBytes()
{ {
try { try {
return ((StringBuffer)_buf).toString().getBytes(_usesUnicode ? return ((CharSequence)_buf).toString().getBytes(_usesUnicode ?
"UTF-16LE" : "Cp1252"); "UTF-16LE" : "Cp1252");
} catch (UnsupportedEncodingException ignore) { } catch (UnsupportedEncodingException ignore) {
throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!"); 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<TextPiece>
*/ */
public String substring(int start, int end) public String substring(int start, int end)
{ {
StringBuffer buf = (StringBuffer)_buf; StringBuilder buf = (StringBuilder)_buf;
// Validate // Validate
if(start < 0) { if(start < 0) {
@ -167,7 +173,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
int bufStart = overlapStart - myStart; int bufStart = overlapStart - myStart;
int bufEnd = overlapEnd - 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 // We need to invoke this even if text from this piece is not being
@ -197,7 +203,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
if (limitsAreEqual(o)) if (limitsAreEqual(o))
{ {
TextPiece tp = (TextPiece)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); tp._usesUnicode == _usesUnicode && _pd.equals(tp._pd);
} }
return false; return false;

View File

@ -264,7 +264,7 @@ public class TextPieceTable implements CharIndexTranslator
StringBuilder docText = new StringBuilder(); StringBuilder docText = new StringBuilder();
for ( TextPiece textPiece : _textPieces ) for ( TextPiece textPiece : _textPieces )
{ {
String toAppend = textPiece.getStringBuffer().toString(); String toAppend = textPiece.getStringBuilder().toString();
int toAppendLength = toAppend.length(); int toAppendLength = toAppend.length();
if ( toAppendLength != textPiece.getEnd() - textPiece.getStart() ) if ( toAppendLength != textPiece.getEnd() - textPiece.getStart() )

View File

@ -70,13 +70,13 @@ public final class TestTextPieceTable extends TestCase {
// All ascii, so stored in one big lump // All ascii, so stored in one big lump
assertEquals(1, tbl.getTextPieces().size()); assertEquals(1, tbl.getTextPieces().size());
TextPiece tp = (TextPiece)tbl.getTextPieces().get(0); TextPiece tp = tbl.getTextPieces().get(0);
assertEquals(0, tp.getStart()); assertEquals(0, tp.getStart());
assertEquals(339, tp.getEnd()); assertEquals(339, tp.getEnd());
assertEquals(339, tp.characterLength()); assertEquals(339, tp.characterLength());
assertEquals(339, tp.bytesLength()); 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 // Save and re-load
@ -84,13 +84,13 @@ public final class TestTextPieceTable extends TestCase {
tbl = docB.getTextTable(); tbl = docB.getTextTable();
assertEquals(1, tbl.getTextPieces().size()); assertEquals(1, tbl.getTextPieces().size());
tp = (TextPiece)tbl.getTextPieces().get(0); tp = tbl.getTextPieces().get(0);
assertEquals(0, tp.getStart()); assertEquals(0, tp.getStart());
assertEquals(339, tp.getEnd()); assertEquals(339, tp.getEnd());
assertEquals(339, tp.characterLength()); assertEquals(339, tp.characterLength());
assertEquals(339, tp.bytesLength()); 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"));
} }
/** /**