本文整理了Java中android.os.Parcel.dataPosition()
方法的一些代码示例,展示了Parcel.dataPosition()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcel.dataPosition()
方法的具体详情如下:
包路径:android.os.Parcel
类名称:Parcel
方法名:dataPosition
[英]Returns the current position in the parcel data. Never more than #dataSize.
[中]返回地块数据中的当前位置。永远不要超过#数据大小。
代码示例来源:origin: k9mail/k-9
public OpenPgpError createFromParcel(final Parcel source) {
source.readInt(); // parcelableVersion
int parcelableSize = source.readInt();
int startPosition = source.dataPosition();
OpenPgpError error = new OpenPgpError();
error.errorId = source.readInt();
error.message = source.readString();
// skip over all fields added in future versions of this parcel
source.setDataPosition(startPosition + parcelableSize);
return error;
}
代码示例来源:origin: k9mail/k-9
public OpenPgpMetadata createFromParcel(final Parcel source) {
int version = source.readInt(); // parcelableVersion
int parcelableSize = source.readInt();
int startPosition = source.dataPosition();
OpenPgpMetadata vr = new OpenPgpMetadata();
vr.filename = source.readString();
vr.mimeType = source.readString();
vr.modificationTime = source.readLong();
vr.originalSize = source.readLong();
if (version >= 2) {
vr.charset = source.readString();
}
// skip over all fields added in future versions of this parcel
source.setDataPosition(startPosition + parcelableSize);
return vr;
}
代码示例来源:origin: k9mail/k-9
public AutocryptPeerUpdate createFromParcel(final Parcel source) {
int version = source.readInt(); // parcelableVersion
int parcelableSize = source.readInt();
int startPosition = source.dataPosition();
AutocryptPeerUpdate vr = new AutocryptPeerUpdate(source, version);
// skip over all fields added in future versions of this parcel
source.setDataPosition(startPosition + parcelableSize);
return vr;
}
代码示例来源:origin: k9mail/k-9
public OpenPgpSignatureResult createFromParcel(final Parcel source) {
int version = source.readInt(); // parcelableVersion
int parcelableSize = source.readInt();
int startPosition = source.dataPosition();
OpenPgpSignatureResult vr = new OpenPgpSignatureResult(source, version);
// skip over all fields added in future versions of this parcel
source.setDataPosition(startPosition + parcelableSize);
return vr;
}
代码示例来源:origin: k9mail/k-9
public OpenPgpDecryptionResult createFromParcel(final Parcel source) {
int version = source.readInt(); // parcelableVersion
int parcelableSize = source.readInt();
int startPosition = source.dataPosition();
int result = source.readInt();
byte[] sessionKey = version > 1 ? source.createByteArray() : null;
byte[] decryptedSessionKey = version > 1 ? source.createByteArray() : null;
OpenPgpDecryptionResult vr = new OpenPgpDecryptionResult(result, sessionKey, decryptedSessionKey);
// skip over all fields added in future versions of this parcel
source.setDataPosition(startPosition + parcelableSize);
return vr;
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testDataPositionAfterSomeWrites() {
parcel.writeInt(1);
assertThat(parcel.dataPosition()).isEqualTo(4);
parcel.writeFloat(5);
assertThat(parcel.dataPosition()).isEqualTo(8);
parcel.writeDouble(37);
assertThat(parcel.dataPosition()).isEqualTo(16);
parcel.writeStrongBinder(new Binder()); // 20 bytes
assertThat(parcel.dataPosition()).isEqualTo(36);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testWriteNullString() {
parcel.writeString(null);
parcel.setDataPosition(0);
assertThat(parcel.readString()).isNull();
assertThat(parcel.dataPosition()).isEqualTo(4);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testSetDataPositionToEmptyString() {
parcel.writeString("");
parcel.setDataPosition(parcel.dataPosition());
parcel.writeString("something else");
parcel.setDataPosition(0);
assertThat(parcel.readString()).isEmpty();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testReadIntWhenEmpty() {
assertThat(parcel.readInt()).isEqualTo(0);
assertThat(parcel.dataPosition()).isEqualTo(0);
assertInvariants();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testReadLongWhenEmpty() {
assertThat(parcel.readLong()).isEqualTo(0);
assertThat(parcel.dataPosition()).isEqualTo(0);
assertInvariants();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testDataPositionAfterSomeReads() {
parcel.writeInt(1);
parcel.writeFloat(5);
parcel.writeDouble(37);
parcel.setDataPosition(0);
parcel.readInt();
assertThat(parcel.dataPosition()).isEqualTo(4);
parcel.readFloat();
assertThat(parcel.dataPosition()).isEqualTo(8);
parcel.readDouble();
assertThat(parcel.dataPosition()).isEqualTo(16);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testByteArrayOfZeroesReadAsZeroes() {
parcel.writeByteArray(new byte[15]);
assertThat(parcel.dataSize()).isEqualTo(20);
parcel.setDataPosition(0);
assertThat(parcel.readInt()).isEqualTo(15);
assertThat(parcel.readLong()).isEqualTo(0);
assertThat(parcel.readLong()).isEqualTo(0);
assertThat(parcel.dataPosition()).isEqualTo(20);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testUnmarshallEmpty() throws IOException {
// Unmarshall an zero-length byte string, although, pass a non-empty array to make sure the
// length/offset are respected.
parcel.unmarshall(new byte[] {1, 2, 3}, 1, 0);
assertThat(parcel.dataSize()).isEqualTo(0);
assertThat(parcel.dataPosition()).isEqualTo(0);
// Should not throw "Did you forget to setDataPosition(0)?" because it's still empty.
assertThat(parcel.readInt()).isEqualTo(0);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testZeroCanBeCasted_8ByteTypesCanBeReadAs4Bytes() {
parcel.writeLong(0);
parcel.writeDouble(0.0);
parcel.writeLong(0);
assertThat(parcel.dataSize()).named("total size").isEqualTo(24);
parcel.setDataPosition(0);
assertThat(parcel.readInt()).isEqualTo(0);
assertThat(parcel.readFloat()).isEqualTo(0.0f);
assertThat(parcel.createByteArray()).isEqualTo(new byte[0]);
assertThat(parcel.dataPosition()).isEqualTo(12);
assertThat(parcel.readInt()).isEqualTo(0);
assertThat(parcel.readFloat()).isEqualTo(0.0f);
assertThat(parcel.createByteArray()).isEqualTo(new byte[0]);
assertThat(parcel.dataPosition()).isEqualTo(24);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testFullyOverwritten() {
parcel.writeInt(1);
// NOTE: Later, this 8-byte long gets chopped up by two 4-byte writes, but it's OK because this
// byte range is not read until it has been fully overwritten.
parcel.writeLong(5);
parcel.writeInt(4);
assertInvariants();
parcel.setDataPosition(4);
parcel.writeByte((byte) 55); // Byte and int have the parceled size.
parcel.writeString(null); // And so does a null string.
assertInvariants();
parcel.setDataPosition(0);
assertThat(parcel.readInt()).named("readInt@0").isEqualTo(1);
assertThat(parcel.dataPosition()).named("position post-readInt@0").isEqualTo(4);
assertThat(parcel.readByte()).named("readByte@4").isEqualTo(55);
assertThat(parcel.dataPosition()).named("position post-readByte@4").isEqualTo(8);
assertThat(parcel.readString()).named("readString@8").isNull();
assertThat(parcel.dataPosition()).named("position post-readString@8").isEqualTo(12);
assertThat(parcel.readInt()).named("readInt@12").isEqualTo(4);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testZeroCanBeCasted_4ByteTypesCanBeReadAs8Bytes() {
parcel.writeByte((byte) 0);
parcel.writeByte((byte) 0);
parcel.writeInt(0);
parcel.writeInt(0);
parcel.writeFloat(0.0f);
parcel.writeByteArray(new byte[0]);
assertThat(parcel.dataSize()).named("total size").isEqualTo(24);
parcel.setDataPosition(0);
assertThat(parcel.readLong()).isEqualTo(0L);
assertWithMessage("long consumes 8B").that(parcel.dataPosition()).isEqualTo(8);
assertThat(parcel.readDouble()).isEqualTo(0.0);
assertWithMessage("double consumes 8B").that(parcel.dataPosition()).isEqualTo(16);
assertThat(parcel.readString()).isEqualTo("");
assertWithMessage("empty string 8B").that(parcel.dataPosition()).isEqualTo(24);
}
代码示例来源:origin: robolectric/robolectric
@Test
public void testReadWriteNumbers() {
parcel.writeInt(Integer.MIN_VALUE);
assertThat(parcel.dataSize()).isEqualTo(4);
parcel.writeLong(Long.MAX_VALUE);
assertThat(parcel.dataSize()).isEqualTo(12);
double d = 3.14159;
parcel.writeDouble(d);
assertThat(parcel.dataSize()).isEqualTo(20);
float f = -6.022e23f;
parcel.writeFloat(f);
assertThat(parcel.dataSize()).isEqualTo(24);
assertInvariants();
parcel.setDataPosition(0);
assertThat(parcel.readInt()).isEqualTo(Integer.MIN_VALUE);
assertThat(parcel.dataPosition()).isEqualTo(4);
assertThat(parcel.readLong()).isEqualTo(Long.MAX_VALUE);
assertThat(parcel.dataPosition()).isEqualTo(12);
assertThat(parcel.readDouble()).isEqualTo(d);
assertThat(parcel.dataPosition()).isEqualTo(20);
assertThat(parcel.readFloat()).isEqualTo(f);
assertThat(parcel.dataPosition()).isEqualTo(24);
assertWithMessage("read past end is valid").that(parcel.readInt()).isEqualTo(0);
assertThat(parcel.dataPosition()).isEqualTo(24);
assertInvariants();
}
代码示例来源: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: robolectric/robolectric
@Test
public void testSetDataSize() {
parcel.writeInt(1);
parcel.writeInt(2);
parcel.writeInt(3);
parcel.writeInt(4);
parcel.writeInt(5);
assertThat(parcel.dataSize()).isEqualTo(20);
assertInvariants();
int oldCapacity = parcel.dataCapacity();
parcel.setDataSize(12);
assertWithMessage("should equal requested size").that(parcel.dataSize()).isEqualTo(12);
assertWithMessage("position gets truncated").that(parcel.dataPosition()).isEqualTo(12);
assertWithMessage("capacity doesn't shrink").that(parcel.dataCapacity()).isEqualTo(oldCapacity);
parcel.setDataSize(100);
assertWithMessage("should equal requested size").that(parcel.dataSize()).isEqualTo(100);
assertWithMessage("position untouched").that(parcel.dataPosition()).isEqualTo(12);
assertInvariants();
}
代码示例来源:origin: robolectric/robolectric
private void assertInvariants(Parcel p) {
assertWithMessage("capacity >= size").that(p.dataCapacity()).isAtLeast(p.dataSize());
assertWithMessage("position <= size").that(p.dataPosition()).isAtMost(p.dataSize());
assertWithMessage("available = size - position")
.that(p.dataAvail())
.isEqualTo(p.dataSize() - p.dataPosition());
assertWithMessage("size % 4 == 0").that(p.dataSize() % 4).isEqualTo(0);
assertWithMessage("capacity % 4 == 0").that(p.dataSize() % 4).isEqualTo(0);
}
}
内容来源于网络,如有侵权,请联系作者删除!