android.os.Parcel.marshall()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(460)

本文整理了Java中android.os.Parcel.marshall()方法的一些代码示例,展示了Parcel.marshall()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcel.marshall()方法的具体详情如下:
包路径:android.os.Parcel
类名称:Parcel
方法名:marshall

Parcel.marshall介绍

[英]Returns the raw bytes of the parcel.

The data you retrieve here must not be placed in any kind of persistent storage (on local disk, across a network, etc). For that, you should use standard serialization or another kind of general serialization mechanism. The Parcel marshalled representation is highly optimized for local IPC, and as such does not attempt to maintain compatibility with data created in different versions of the platform.
[中]返回包裹的原始字节。
您在此处检索的数据不得放在任何类型的永久性存储中(在本地磁盘上、通过网络等)。为此,应该使用标准序列化或其他类型的通用序列化机制。包裹编组表示法针对本地IPC进行了高度优化,因此不会试图保持与在不同版本平台中创建的数据的兼容性。

代码示例

代码示例来源:origin: android-hacker/VirtualXposed

public static void writeParcelToFile(Parcel p, File file) throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  fos.write(p.marshall());
  fos.close();
}

代码示例来源:origin: mttkay/ignition

/**
 * @see com.github.droidfu.cachefu.AbstractCache#writeValueToDisk(java.io.File,
 *      java.lang.Object)
 */
@Override
protected void writeValueToDisk(File file, CachedModel data) throws IOException {
  // Write object into parcel
  Parcel parcelOut = Parcel.obtain();
  parcelOut.writeString(data.getClass().getCanonicalName());
  parcelOut.writeParcelable(data, 0);
  // Write byte data to file
  FileOutputStream ostream = new FileOutputStream(file);
  BufferedOutputStream bistream = new BufferedOutputStream(ostream);
  bistream.write(parcelOut.marshall());
  bistream.close();
}

代码示例来源:origin: android-hacker/VirtualXposed

private void saveJobs() {
  File jobFile = VEnvironment.getJobConfigFile();
  Parcel p = Parcel.obtain();
  try {
    p.writeInt(JOB_FILE_VERSION);
    p.writeInt(mJobStore.size());
    for (Map.Entry<JobId, JobConfig> entry : mJobStore.entrySet()) {
      entry.getKey().writeToParcel(p, 0);
      entry.getValue().writeToParcel(p, 0);
    }
    FileOutputStream fos = new FileOutputStream(jobFile);
    fos.write(p.marshall());
    fos.close();
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    p.recycle();
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

/**
 * Serializing all accounts
 */
private void saveAllAccounts() {
  File accountFile = VEnvironment.getAccountConfigFile();
  Parcel dest = Parcel.obtain();
  try {
    dest.writeInt(1);
    List<VAccount> accounts = new ArrayList<>();
    for (int i = 0; i < this.accountsByUserId.size(); i++) {
      List<VAccount> list = this.accountsByUserId.valueAt(i);
      if (list != null) {
        accounts.addAll(list);
      }
    }
    dest.writeInt(accounts.size());
    for (VAccount account : accounts) {
      account.writeToParcel(dest, 0);
    }
    dest.writeLong(lastAccountChangeTime);
    FileOutputStream fileOutputStream = new FileOutputStream(accountFile);
    fileOutputStream.write(dest.marshall());
    fileOutputStream.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
  dest.recycle();
}

代码示例来源:origin: android-hacker/VirtualXposed

public void save() {
  Parcel p = Parcel.obtain();
  try {
    writeMagic(p);
    p.writeInt(getCurrentVersion());
    writePersistenceData(p);
    FileOutputStream fos = new FileOutputStream(mPersistenceFile);
    fos.write(p.marshall());
    fos.close();
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    p.recycle();
  }
}

代码示例来源:origin: facebook/facebook-android-sdk

private static String encodeUserInfo(UserInfo userInfo) {
    Parcel parcel = Parcel.obtain();
    parcel.writeValue(userInfo);
    byte[] data = parcel.marshall();
    parcel.recycle();
    return Base64.encodeToString(data, Base64.DEFAULT);
  }
}

代码示例来源:origin: konmik/nucleus

when(parcel.marshall()).thenReturn(new byte[0]);

代码示例来源:origin: commonsguy/cw-omnibus

public static byte[] toByteArray(Parcelable parcelable) {
 Parcel parcel=Parcel.obtain();
 parcelable.writeToParcel(parcel, 0);
 byte[] result=parcel.marshall();
 parcel.recycle();
 return(result);
}

代码示例来源:origin: k9mail/k-9

public static byte[] marshall(Parcelable parceable) {
  Parcel parcel = Parcel.obtain();
  parceable.writeToParcel(parcel, 0);
  byte[] bytes = parcel.marshall();
  parcel.recycle();
  return bytes;
}

代码示例来源:origin: konmik/nucleus

static byte[] marshall(Object o) {
    Parcel parcel = Parcel.obtain();
    parcel.writeValue(o);
    byte[] result = parcel.marshall();
    parcel.recycle();
    return result;
  }
}

代码示例来源:origin: robolectric/robolectric

/**
 * Sets active playback configurations that will be served by {@link
 * AudioManager#getActivePlaybackConfigurations}.
 *
 * <p>Note that there is no public {@link AudioPlaybackConfiguration} constructor, so the
 * configurations returned are specified by their audio attributes only.
 */
@TargetApi(VERSION_CODES.O)
public void setActivePlaybackConfigurationsFor(List<AudioAttributes> audioAttributes) {
 activePlaybackConfigurations = new ArrayList<>(audioAttributes.size());
 for (AudioAttributes audioAttribute : audioAttributes) {
  Parcel p = Parcel.obtain();
  p.writeInt(0); // mPlayerIId
  p.writeInt(0); // mPlayerType
  p.writeInt(0); // mClientUid
  p.writeInt(0); // mClientPid
  p.writeInt(AudioPlaybackConfiguration.PLAYER_STATE_STARTED); // mPlayerState
  audioAttribute.writeToParcel(p, 0);
  p.writeStrongInterface(null);
  byte[] bytes = p.marshall();
  p.recycle();
  p = Parcel.obtain();
  p.unmarshall(bytes, 0, bytes.length);
  p.setDataPosition(0);
  AudioPlaybackConfiguration configuration =
    AudioPlaybackConfiguration.CREATOR.createFromParcel(p);
  p.recycle();
  activePlaybackConfigurations.add(configuration);
 }
}

代码示例来源:origin: android-hacker/VirtualXposed

pkg.writeToParcel(p, 0);
  FileOutputStream fos = new FileOutputStream(VEnvironment.getPackageCacheFile(packageName));
  fos.write(p.marshall());
  fos.close();
} catch (Exception e) {

代码示例来源:origin: robolectric/robolectric

@Test
public void testMarshallFailsFastReadingInterruptedObject() {
 parcel.writeString("hello all");
 parcel.setDataPosition(4);
 parcel.writeInt(1);
 try {
  parcel.marshall();
  fail();
 } catch (UnreliableBehaviorError e) {
  assertThat(e)
    .hasMessage(
      "Looking for Object at position 0, found String [hello all] taking 24 bytes, but "
        + "[1] interrupts it at position 4");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testMarshallFailsFastReadingTruncatedObject() {
 parcel.writeString("hello all");
 parcel.setDataSize(8);
 try {
  parcel.marshall();
  fail();
 } catch (UnreliableBehaviorError e) {
  assertThat(e)
    .hasMessage(
      "Looking for Object at position 0, found String [hello all] taking 24 bytes, but "
        + "[uninitialized data or the end of the buffer] interrupts it at position 8");
 }
}

代码示例来源:origin: facebook/facebook-android-sdk

public static <E extends Parcelable> E parcelAndUnparcel(final E object) {
  final Parcel writeParcel = Parcel.obtain();
  final Parcel readParcel = Parcel.obtain();
  try {
    writeParcel.writeParcelable(object, 0);
    final byte[] bytes = writeParcel.marshall();
    readParcel.unmarshall(bytes, 0, bytes.length);
    readParcel.setDataPosition(0);
    return readParcel.readParcelable(object.getClass().getClassLoader());
  } finally {
    writeParcel.recycle();
    readParcel.recycle();
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testMarshallFailsFastReadingUninitializedData() {
 parcel.writeString("hello everyone");
 parcel.setDataSize(parcel.dataSize() + 4);
 parcel.setDataPosition(parcel.dataSize());
 parcel.writeInt(1);
 try {
  parcel.marshall();
  fail();
 } catch (UnreliableBehaviorError e) {
  assertThat(e).hasMessage("Reading uninitialized data at position 36");
 }
}

代码示例来源:origin: robolectric/robolectric

.isEqualTo("str1");
byte[] data = parcel.marshall();
Parcel parcel2 = Parcel.obtain();
parcel2.unmarshall(data, 0, data.length);

代码示例来源:origin: robolectric/robolectric

@Test
public void testMarshallAndUnmarshall() {
 parcel.writeInt(1);
 parcel.writeString("hello");
 parcel.writeDouble(25.0);
 parcel.writeFloat(1.25f);
 parcel.writeByte((byte) 0xAF);
 int oldSize = parcel.dataSize();
 parcel.setDataPosition(7);
 byte[] rawBytes = parcel.marshall();
 assertWithMessage("data position preserved").that(parcel.dataPosition()).isEqualTo(7);
 Parcel parcel2 = Parcel.obtain();
 assertInvariants(parcel2);
 parcel2.unmarshall(rawBytes, 0, rawBytes.length);
 assertThat(parcel2.dataPosition()).isEqualTo(parcel2.dataSize());
 parcel2.setDataPosition(0);
 assertThat(parcel2.dataSize()).isEqualTo(oldSize);
 assertThat(parcel2.readInt()).isEqualTo(1);
 assertThat(parcel2.readString()).isEqualTo("hello");
 assertThat(parcel2.readDouble()).isEqualTo(25.0);
 assertThat(parcel2.readFloat()).isEqualTo(1.25f);
 assertThat(parcel2.readByte()).isEqualTo((byte) 0xAF);
}

代码示例来源:origin: stackoverflow.com

Parcel p = Parcel.obtain();
objLocation.writeToParcel(p, 0);
final byte[] b = p.marshall();      //now you've got bytes
p.recycle();

代码示例来源:origin: cn.leancloud.android/avoscloud-statistics

private static byte[] marshall(Parcelable parcelable) {
 Parcel outer = Parcel.obtain();
 parcelable.writeToParcel(outer, 0);
 byte[] data = outer.marshall();
 return data;
}

相关文章

Parcel类方法