FileDocCategorySizeDatePackage
FastXmlSerializer.javaAPI DocAndroid 1.5 API12334Wed May 06 22:41:56 BST 2009com.android.internal.util

FastXmlSerializer

public class FastXmlSerializer extends Object implements XmlSerializer
This is a quick and dirty implementation of XmlSerializer that isn't horribly painfully slow like the normal one. It only does what is needed for the specific XML files being written with it.

Fields Summary
private static final String[]
ESCAPE_TABLE
private static final int
BUFFER_LEN
private final char[]
mText
private int
mPos
private Writer
mWriter
private OutputStream
mOutputStream
private CharsetEncoder
mCharset
private ByteBuffer
mBytes
private boolean
mInTag
Constructors Summary
Methods Summary
private voidappend(char c)


          
        int pos = mPos;
        if (pos >= (BUFFER_LEN-1)) {
            flush();
            pos = mPos;
        }
        mText[pos] = c;
        mPos = pos+1;
    
private voidappend(java.lang.String str, int i, int length)

        if (length > BUFFER_LEN) {
            final int end = i + length;
            while (i < end) {
                int next = i + BUFFER_LEN;
                append(str, i, next<end ? BUFFER_LEN : (end-i));
                i = next;
            }
            return;
        }
        int pos = mPos;
        if ((pos+length) > BUFFER_LEN) {
            flush();
            pos = mPos;
        }
        str.getChars(i, i+length, mText, pos);
        mPos = pos + length;
    
private voidappend(char[] buf, int i, int length)

        if (length > BUFFER_LEN) {
            final int end = i + length;
            while (i < end) {
                int next = i + BUFFER_LEN;
                append(buf, i, next<end ? BUFFER_LEN : (end-i));
                i = next;
            }
            return;
        }
        int pos = mPos;
        if ((pos+length) > BUFFER_LEN) {
            flush();
            pos = mPos;
        }
        System.arraycopy(buf, i, mText, pos, length);
        mPos = pos + length;
    
private voidappend(java.lang.String str)

        append(str, 0, str.length());
    
public org.xmlpull.v1.XmlSerializerattribute(java.lang.String namespace, java.lang.String name, java.lang.String value)

        append(' ");
        if (namespace != null) {
            append(namespace);
            append(':");
        }
        append(name);
        append("=\"");

        escapeAndAppendString(value);
        append('"");
        return this;
    
public voidcdsect(java.lang.String text)

        throw new UnsupportedOperationException();
    
public voidcomment(java.lang.String text)

        throw new UnsupportedOperationException();
    
public voiddocdecl(java.lang.String text)

        throw new UnsupportedOperationException();
    
public voidendDocument()

        flush();
    
public org.xmlpull.v1.XmlSerializerendTag(java.lang.String namespace, java.lang.String name)

        if (mInTag) {
            append(" />\n");
        } else {
            append("</");
            if (namespace != null) {
                append(namespace);
                append(':");
            }
            append(name);
            append(">\n");
        }
        mInTag = false;
        return this;
    
public voidentityRef(java.lang.String text)

        throw new UnsupportedOperationException();
    
private voidescapeAndAppendString(java.lang.String string)

        final int N = string.length();
        final char NE = (char)ESCAPE_TABLE.length;
        final String[] escapes = ESCAPE_TABLE;
        int lastPos = 0;
        int pos;
        for (pos=0; pos<N; pos++) {
            char c = string.charAt(pos);
            if (c >= NE) continue;
            String escape = escapes[c];
            if (escape == null) continue;
            if (lastPos < pos) append(string, lastPos, pos-lastPos);
            lastPos = pos + 1;
            append(escape);
        }
        if (lastPos < pos) append(string, lastPos, pos-lastPos);
    
private voidescapeAndAppendString(char[] buf, int start, int len)

        final char NE = (char)ESCAPE_TABLE.length;
        final String[] escapes = ESCAPE_TABLE;
        int end = start+len;
        int lastPos = start;
        int pos;
        for (pos=start; pos<end; pos++) {
            char c = buf[pos];
            if (c >= NE) continue;
            String escape = escapes[c];
            if (escape == null) continue;
            if (lastPos < pos) append(buf, lastPos, pos-lastPos);
            lastPos = pos + 1;
            append(escape);
        }
        if (lastPos < pos) append(buf, lastPos, pos-lastPos);
    
public voidflush()

        //Log.i("PackageManager", "flush mPos=" + mPos);
        if (mPos > 0) {
            if (mOutputStream != null) {
                CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
                CoderResult result = mCharset.encode(charBuffer, mBytes, true);
                while (true) {
                    if (result.isError()) {
                        throw new IOException(result.toString());
                    } else if (result.isOverflow()) {
                        flushBytes();
                        result = mCharset.encode(charBuffer, mBytes, true);
                        continue;
                    }
                    break;
                }
                flushBytes();
                mOutputStream.flush();
            } else {
                mWriter.write(mText, 0, mPos);
                mWriter.flush();
            }
            mPos = 0;
        }
    
private voidflushBytes()

        int position;
        if ((position = mBytes.position()) > 0) {
            mBytes.flip();
            mOutputStream.write(mBytes.array(), 0, position);
            mBytes.clear();
        }
    
public intgetDepth()

        throw new UnsupportedOperationException();
    
public booleangetFeature(java.lang.String name)

        throw new UnsupportedOperationException();
    
public java.lang.StringgetName()

        throw new UnsupportedOperationException();
    
public java.lang.StringgetNamespace()

        throw new UnsupportedOperationException();
    
public java.lang.StringgetPrefix(java.lang.String namespace, boolean generatePrefix)

        throw new UnsupportedOperationException();
    
public java.lang.ObjectgetProperty(java.lang.String name)

        throw new UnsupportedOperationException();
    
public voidignorableWhitespace(java.lang.String text)

        throw new UnsupportedOperationException();
    
public voidprocessingInstruction(java.lang.String text)

        throw new UnsupportedOperationException();
    
public voidsetFeature(java.lang.String name, boolean state)

        if (name.equals("http://xmlpull.org/v1/doc/features.html#indent-output")) {
            return;
        }
        throw new UnsupportedOperationException();
    
public voidsetOutput(java.io.OutputStream os, java.lang.String encoding)

        if (os == null)
            throw new IllegalArgumentException();
        if (true) {
            try {
                mCharset = Charset.forName(encoding).newEncoder();
            } catch (IllegalCharsetNameException e) {
                throw (UnsupportedEncodingException) (new UnsupportedEncodingException(
                        encoding).initCause(e));
            } catch (UnsupportedCharsetException e) {
                throw (UnsupportedEncodingException) (new UnsupportedEncodingException(
                        encoding).initCause(e));
            }
            mOutputStream = os;
        } else {
            setOutput(
                encoding == null
                    ? new OutputStreamWriter(os)
                    : new OutputStreamWriter(os, encoding));
        }
    
public voidsetOutput(java.io.Writer writer)

        mWriter = writer;
    
public voidsetPrefix(java.lang.String prefix, java.lang.String namespace)

        throw new UnsupportedOperationException();
    
public voidsetProperty(java.lang.String name, java.lang.Object value)

        throw new UnsupportedOperationException();
    
public voidstartDocument(java.lang.String encoding, java.lang.Boolean standalone)

        append("<?xml version='1.0' encoding='utf-8' standalone='"
                + (standalone ? "yes" : "no") + "' ?>\n");
    
public org.xmlpull.v1.XmlSerializerstartTag(java.lang.String namespace, java.lang.String name)

        if (mInTag) {
            append(">\n");
        }
        append('<");
        if (namespace != null) {
            append(namespace);
            append(':");
        }
        append(name);
        mInTag = true;
        return this;
    
public org.xmlpull.v1.XmlSerializertext(char[] buf, int start, int len)

        if (mInTag) {
            append(">");
            mInTag = false;
        }
        escapeAndAppendString(buf, start, len);
        return this;
    
public org.xmlpull.v1.XmlSerializertext(java.lang.String text)

        if (mInTag) {
            append(">");
            mInTag = false;
        }
        escapeAndAppendString(text);
        return this;