首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

职员年度工作总结报告(2012年度)-张宝华

2013-01-05 
员工年度工作总结报告(2012年度)--张宝华???一.先从Serialize说起? ?? ? 我们都知道JAVA中的Serialize机制

员工年度工作总结报告(2012年度)--张宝华

???一.先从Serialize说起
? ?? ? 我们都知道JAVA中的Serialize机制,译成串行化、序列化……,其作用是能将数据对象存入字节流当中,在需要时重新生成对象。主要应用是利用外部存储设备保存对象状态,以及通过网络传输对象等。

? ?? ??二.Android中的新的序列化机制
? ?? ? 在Android系统中,定位为针对内存受限的设备,因此对性能要求更高,另外系统中采用了新的IPC(进程间通信)机制,必然要求使用性能更出色的对象传输方式。在这样的环境下,Parcel被设计出来,其定位就是轻量级的高效的对象序列化和反序列化机制。

? ?? ??三.Parcel类的背后
? ?? ? 在Framework中有parcel类,源码路径是:Frameworks/base/core/java/android/os/Parcel.java
? ?? ? 典型的源码片断如下:

java代码:

  1. finishWrite(padded);
  2. return data;
  3. }?

  4. status_t err = growData(padded);
  5. if (err == NO_ERROR) goto restart_write;
  6. return NULL;
  7. }?

  8. status_t Parcel::writeInt32(int32_t val)
  9. {
  10. return writeAligned(val);
  11. }?

  12. status_t Parcel::writeInt64(int64_t val)
  13. {
  14. return writeAligned(val);
  15. }?

  16. status_t Parcel::writeFloat(float val)
  17. {
  18. return writeAligned(val);
  19. }?

  20. status_t Parcel::writeDouble(double val)
  21. {
  22. return writeAligned(val);
  23. }?

  24. status_t Parcel::writeIntPtr(intptr_t val)
  25. {
  26. return writeAligned(val);
  27. }?

  28. status_t Parcel::writeCString(const char* str)
  29. {
  30. return write(str, strlen(str)+1);
  31. }?

  32. status_t Parcel::writeString8(const String8& str)
  33. {
  34. status_t err = writeInt32(str.bytes());
  35. if (err == NO_ERROR) {
  36. err = write(str.string(), str.bytes()+1);
  37. }
  38. return err;
  39. }?

  40. status_t Parcel::writeString16(const String16& str)
  41. {
  42. return writeString16(str.string(), str.size());
  43. }?

  44. status_t Parcel::writeString16(const char16_t* str, size_t len)
  45. {
  46. if (str == NULL) return writeInt32(-1);?

  47. status_t err = writeInt32(len);
  48. if (err == NO_ERROR) {
  49. len *= sizeof(char16_t);
  50. uint8_t* data = (uint8_t*)writeInplace(len+sizeof(char16_t));
  51. if (data) {
  52. memcpy(data, str, len);
  53. *reinterpret_cast(data+len) = 0;
  54. return NO_ERROR;
  55. }
  56. err = mError;
  57. }
  58. return err;
  59. }?

  60. status_t Parcel::writeStrongBinder(const sp& val)
  61. {
  62. return flatten_binder(ProcessState::self(), val, this);
  63. }?

  64. status_t Parcel::writeWeakBinder(const wp& val)
  65. {
  66. return flatten_binder(ProcessState::self(), val, this);
  67. }?

  68. status_t Parcel::writeNativeHandle(const native_handle* handle)
  69. {
  70. if (!handle || handle->version != sizeof(native_handle))
  71. return BAD_TYPE;?

  72. status_t err;
  73. err = writeInt32(handle->numFds);
  74. if (err != NO_ERROR) return err;?

  75. err = writeInt32(handle->numInts);
  76. if (err != NO_ERROR) return err;?

  77. for (int i=0 ; err==NO_ERROR && inumFds ; i++)
  78. err = writeDupFileDescriptor(handle->data[i]);?

  79. if (err != NO_ERROR) {
  80. LOGD(“write native handle, write dup fd failed”);
  81. return err;
  82. }
  83. err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
  84. return err;
  85. }?

  86. status_t Parcel::writeFileDescriptor(int fd)
  87. {
  88. flat_binder_object obj;
  89. obj.type = BINDER_TYPE_FD;
  90. obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  91. obj.handle = fd;
  92. obj.cookie = (void*)0;
  93. return writeObject(obj, true);
  94. }?

  95. status_t Parcel::writeDupFileDescriptor(int fd)
  96. {
  97. flat_binder_object obj;
  98. obj.type = BINDER_TYPE_FD;
  99. obj.flags = 0x7f | FLAT_BINDER_FLAG_ACCEPTS_FDS;
  100. obj.handle = dup(fd);
  101. obj.cookie = (void*)1;
  102. return writeObject(obj, true);
  103. }?

  104. status_t Parcel::write(const Flattenable& val)
  105. {
  106. status_t err;?

  107. // size if needed
  108. size_t len = val.getFlattenedSize();
  109. size_t fd_count = val.getFdCount();?

  110. err = this->writeInt32(len);
  111. if (err) return err;?

  112. err = this->writeInt32(fd_count);
  113. if (err) return err;?

  114. // payload
  115. void* buf = this->writeInplace(PAD_SIZE(len));
  116. if (buf == NULL)
  117. return BAD_VALUE;?

  118. int* fds = NULL;
  119. if (fd_count) {
  120. fds = new int[fd_count];
  121. }?

  122. err = val.flatten(buf, len, fds, fd_count);
  123. for (size_t i=0 ; i err = this->writeDupFileDescriptor( fds[i] );
  124. }?

  125. if (fd_count) {
  126. delete [] fds;
  127. }?

  128. return err;
  129. }?

  130. status_t Parcel::writeObject(const flat_binder_object& val, bool nullMetaData)
  131. {
  132. const bool enoughData = (mDataPos+sizeof(val)) <= mDataCapacity;
  133. const bool enoughObjects = mObjectsSize < mObjectsCapacity;
  134. if (enoughData && enoughObjects) {
  135. restart_write:
  136. *reinterpret_cast(mData+mDataPos) = val;?

  137. // Need to write meta-data?
  138. if (nullMetaData || val.binder != NULL) {
  139. mObjects[mObjectsSize] = mDataPos;
  140. acquire_object(ProcessState::self(), val, this);
  141. mObjectsSize++;
  142. }?

  143. // remember if it’s a file descriptor
  144. if (val.type == BINDER_TYPE_FD) {
  145. mHasFds = mFdsKnown = true;
  146. }?

  147. return finishWrite(sizeof(flat_binder_object));
  148. }?

  149. if (!enoughData) {
  150. const status_t err = growData(sizeof(val));
  151. if (err != NO_ERROR) return err;
  152. }
  153. if (!enoughObjects) {
  154. size_t newSize = ((mObjectsSize+2)*3)/2;
  155. size_t* objects = (size_t*)realloc(mObjects, newSize*sizeof(size_t));
  156. if (objects == NULL) return NO_MEMORY;
  157. mObjects = objects;
  158. mObjectsCapacity = newSize;
  159. }?

复制代码

热点排行