Merge "PersistableBundle: writeToStream/readFromStream"

This commit is contained in:
Meng Wang
2020-01-08 23:47:40 +00:00
committed by Gerrit Code Review
2 changed files with 49 additions and 0 deletions

View File

@@ -35255,7 +35255,9 @@ package android.os {
method public int describeContents();
method @Nullable public android.os.PersistableBundle getPersistableBundle(@Nullable String);
method public void putPersistableBundle(@Nullable String, @Nullable android.os.PersistableBundle);
method @NonNull public static android.os.PersistableBundle readFromStream(@NonNull java.io.InputStream) throws java.io.IOException;
method public void writeToParcel(android.os.Parcel, int);
method public void writeToStream(@NonNull java.io.OutputStream) throws java.io.IOException;
field @NonNull public static final android.os.Parcelable.Creator<android.os.PersistableBundle> CREATOR;
field public static final android.os.PersistableBundle EMPTY;
}

View File

@@ -16,17 +16,24 @@
package android.os;
import static java.nio.charset.StandardCharsets.UTF_8;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.ArrayMap;
import android.util.proto.ProtoOutputStream;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.XmlUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
/**
@@ -339,4 +346,44 @@ public final class PersistableBundle extends BaseBundle implements Cloneable, Pa
proto.end(token);
}
/**
* Writes the content of the {@link PersistableBundle} to a {@link OutputStream}.
*
* <p>The content can be read by a {@link #readFromStream}.
*
* @see #readFromStream
*/
public void writeToStream(@NonNull OutputStream outputStream) throws IOException {
FastXmlSerializer serializer = new FastXmlSerializer();
serializer.setOutput(outputStream, UTF_8.name());
serializer.startTag(null, "bundle");
try {
saveToXml(serializer);
} catch (XmlPullParserException e) {
throw new IOException(e);
}
serializer.endTag(null, "bundle");
serializer.flush();
}
/**
* Reads a {@link PersistableBundle} from an {@link InputStream}.
*
* <p>The stream must be generated by {@link #writeToStream}.
*
* @see #writeToStream
*/
@NonNull
public static PersistableBundle readFromStream(@NonNull InputStream inputStream)
throws IOException {
try {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(inputStream, UTF_8.name());
parser.next();
return PersistableBundle.restoreFromXml(parser);
} catch (XmlPullParserException e) {
throw new IOException(e);
}
}
}