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 e544419a22
commit d31694057d
6 changed files with 24 additions and 18 deletions

View File

@ -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();

View File

@ -562,7 +562,7 @@ public final class HWPFLister
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
ret = new String[doc.getTextTable().getTextPieces().size()];
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
ret[i].replaceAll("\r", "\ufffe");

View File

@ -68,7 +68,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
_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<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;
try {
if(pd.isUnicode()) {
@ -91,7 +91,7 @@ public final class TextPiece extends PropertyNode<TextPiece>
} 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<TextPiece>
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<TextPiece>
*/
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<TextPiece>
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<TextPiece>
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;

View File

@ -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() )

View File

@ -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"));
}
/**