Packet size in bytes

Is there a way to find out the packet size in bytes? My point is that I am storing ordered lists of objects in bundle

on onSaveInstanceState

.

I need to check if the packet size has been reached, the size limit and prevent any data from being saved and also prevent an exception TransactionTooLarge

.

+3


source to share


2 answers


It seems to me the simplest way:



fun getBundleSizeInBytes(bundle : Bundle) : Int {
  val parcel = Parcel.obtain()
  parcel.writeValue(bundle)

  val bytes = parcel.marshall()
  parcel.recycle()

  return bytes.size
}

      

+3


source


The package class has a dataSize () member, so the same result can be achieved without calling marshall ():



int getBundleSizeInBytes(Bundle bundle) {
    Parcel parcel = Parcel.obtain();
    int size;

    parcel.writeBundle(bundle);
    size = parcel.dataSize();
    parcel.recycle();

    return size;
}

      

0


source







All Articles