Merge "PersistableBundle: writeToStream/readFromStream" am: 0204e4a08e am: 083df1ec90
Change-Id: I8823618f688d781f5a96549da695b45b0f561878
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user