Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e14402b3 authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "PersistableBundle: writeToStream/readFromStream" am: 0204e4a0 am:...

Merge "PersistableBundle: writeToStream/readFromStream" am: 0204e4a0 am: 083df1ec am: 869646b5

Change-Id: I059547d4a8ed075c82cf483b2b226b3e337203c2
parents bdac9311 869646b5
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -35715,7 +35715,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;
  }
+47 −0
Original line number Diff line number Diff line
@@ -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);
        }
    }
}