NativeByteBuffer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * This is the source code of tgnet library v. 1.1
  3. * It is licensed under GNU GPL v. 2 or later.
  4. * You should have received a copy of the license in this archive (see LICENSE).
  5. *
  6. * Copyright Nikolai Kudashov, 2015-2018.
  7. */
  8. #include <memory.h>
  9. #include <stdlib.h>
  10. #include "NativeByteBuffer.h"
  11. #include "FileLog.h"
  12. #include "ByteArray.h"
  13. #include "ConnectionsManager.h"
  14. #include "BuffersStorage.h"
  15. NativeByteBuffer::NativeByteBuffer(uint32_t size) {
  16. #ifdef ANDROID
  17. if (jclass_ByteBuffer != nullptr) {
  18. JNIEnv *env = 0;
  19. if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  20. if (LOGS_ENABLED) DEBUG_E("can't get jnienv");
  21. exit(1);
  22. }
  23. javaByteBuffer = env->CallStaticObjectMethod(jclass_ByteBuffer, jclass_ByteBuffer_allocateDirect, size);
  24. if (javaByteBuffer == nullptr) {
  25. if (LOGS_ENABLED) DEBUG_E("can't create javaByteBuffer");
  26. exit(1);
  27. }
  28. jobject globalRef = env->NewGlobalRef(javaByteBuffer);
  29. env->DeleteLocalRef(javaByteBuffer);
  30. javaByteBuffer = globalRef;
  31. buffer = (uint8_t *) env->GetDirectBufferAddress(javaByteBuffer);
  32. bufferOwner = false;
  33. } else {
  34. #endif
  35. buffer = new uint8_t[size];
  36. bufferOwner = true;
  37. #ifdef ANDROID
  38. }
  39. #endif
  40. if (buffer == nullptr) {
  41. if (LOGS_ENABLED) DEBUG_E("can't allocate NativeByteBuffer buffer");
  42. exit(1);
  43. }
  44. _limit = _capacity = size;
  45. }
  46. NativeByteBuffer::NativeByteBuffer(bool calculate) {
  47. calculateSizeOnly = calculate;
  48. }
  49. NativeByteBuffer::NativeByteBuffer(uint8_t *buff, uint32_t length) {
  50. buffer = buff;
  51. sliced = true;
  52. _limit = _capacity = length;
  53. }
  54. NativeByteBuffer::~NativeByteBuffer() {
  55. #ifdef ANDROID
  56. if (javaByteBuffer != nullptr) {
  57. JNIEnv *env = 0;
  58. if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  59. if (LOGS_ENABLED) DEBUG_E("can't get jnienv");
  60. exit(1);
  61. }
  62. env->DeleteGlobalRef(javaByteBuffer);
  63. javaByteBuffer = nullptr;
  64. }
  65. #endif
  66. if (bufferOwner && !sliced && buffer != nullptr) {
  67. delete[] buffer;
  68. buffer = nullptr;
  69. }
  70. }
  71. uint32_t NativeByteBuffer::position() {
  72. return _position;
  73. }
  74. void NativeByteBuffer::position(uint32_t position) {
  75. if (position > _limit) {
  76. return;
  77. }
  78. _position = position;
  79. }
  80. uint32_t NativeByteBuffer::capacity() {
  81. return _capacity;
  82. }
  83. uint32_t NativeByteBuffer::limit() {
  84. return _limit;
  85. }
  86. uint32_t NativeByteBuffer::remaining() {
  87. return _limit - _position;
  88. }
  89. void NativeByteBuffer::clearCapacity() {
  90. if (!calculateSizeOnly) {
  91. return;
  92. }
  93. _capacity = 0;
  94. }
  95. void NativeByteBuffer::limit(uint32_t limit) {
  96. if (limit > _capacity) {
  97. return;
  98. }
  99. if (_position > limit) {
  100. _position = limit;
  101. }
  102. _limit = limit;
  103. }
  104. void NativeByteBuffer::flip() {
  105. _limit = _position;
  106. _position = 0;
  107. }
  108. void NativeByteBuffer::clear() {
  109. _position = 0;
  110. _limit = _capacity;
  111. }
  112. uint8_t *NativeByteBuffer::bytes() {
  113. return buffer;
  114. }
  115. void NativeByteBuffer::rewind() {
  116. _position = 0;
  117. }
  118. void NativeByteBuffer::compact() {
  119. if (_position == _limit) {
  120. return;
  121. }
  122. memmove(buffer, buffer + _position, sizeof(uint8_t) * (_limit - _position));
  123. _position = (_limit - _position);
  124. _limit = _capacity;
  125. }
  126. bool NativeByteBuffer::hasRemaining() {
  127. return _position < _limit;
  128. }
  129. void NativeByteBuffer::skip(uint32_t length) {
  130. if (!calculateSizeOnly) {
  131. if (_position + length > _limit) {
  132. return;
  133. }
  134. _position += length;
  135. } else {
  136. _capacity += length;
  137. }
  138. }
  139. void NativeByteBuffer::writeInt32(int32_t x, bool *error) {
  140. if (!calculateSizeOnly) {
  141. if (_position + 4 > _limit) {
  142. if (error != nullptr) {
  143. *error = true;
  144. }
  145. if (LOGS_ENABLED) DEBUG_E("write int32 error");
  146. return;
  147. }
  148. buffer[_position++] = (uint8_t) x;
  149. buffer[_position++] = (uint8_t) (x >> 8);
  150. buffer[_position++] = (uint8_t) (x >> 16);
  151. buffer[_position++] = (uint8_t) (x >> 24);
  152. } else {
  153. _capacity += 4;
  154. }
  155. }
  156. void NativeByteBuffer::writeInt64(int64_t x, bool *error) {
  157. if (!calculateSizeOnly) {
  158. if (_position + 8 > _limit) {
  159. if (error != nullptr) {
  160. *error = true;
  161. }
  162. if (LOGS_ENABLED) DEBUG_E("write int64 error");
  163. return;
  164. }
  165. buffer[_position++] = (uint8_t) x;
  166. buffer[_position++] = (uint8_t) (x >> 8);
  167. buffer[_position++] = (uint8_t) (x >> 16);
  168. buffer[_position++] = (uint8_t) (x >> 24);
  169. buffer[_position++] = (uint8_t) (x >> 32);
  170. buffer[_position++] = (uint8_t) (x >> 40);
  171. buffer[_position++] = (uint8_t) (x >> 48);
  172. buffer[_position++] = (uint8_t) (x >> 56);
  173. } else {
  174. _capacity += 8;
  175. }
  176. }
  177. void NativeByteBuffer::writeBool(bool value, bool *error) {
  178. if (!calculateSizeOnly) {
  179. if (value) {
  180. writeInt32(0x997275b5, error);
  181. } else {
  182. writeInt32(0xbc799737, error);
  183. }
  184. } else {
  185. _capacity += 4;
  186. }
  187. }
  188. void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t length, bool *error) {
  189. if (!calculateSizeOnly) {
  190. if (_position + length > _limit) {
  191. if (error != nullptr) {
  192. *error = true;
  193. }
  194. if (LOGS_ENABLED) DEBUG_E("write bytes error");
  195. return;
  196. }
  197. writeBytesInternal(b, 0, length);
  198. } else {
  199. _capacity += length;
  200. }
  201. }
  202. void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t offset, uint32_t length, bool *error) {
  203. if (!calculateSizeOnly) {
  204. if (_position + length > _limit) {
  205. if (error != nullptr) {
  206. *error = true;
  207. }
  208. if (LOGS_ENABLED) DEBUG_E("write bytes error");
  209. return;
  210. }
  211. writeBytesInternal(b, offset, length);
  212. } else {
  213. _capacity += length;
  214. }
  215. }
  216. void NativeByteBuffer::writeBytes(NativeByteBuffer *b, bool *error) {
  217. uint32_t length = b->_limit - b->_position;
  218. if (length == 0) {
  219. return;
  220. }
  221. if (!calculateSizeOnly) {
  222. if (_position + length > _limit) {
  223. if (error != nullptr) {
  224. *error = true;
  225. }
  226. if (LOGS_ENABLED) DEBUG_E("write bytes error");
  227. return;
  228. }
  229. writeBytesInternal(b->buffer + b->_position, 0, length);
  230. b->position(b->limit());
  231. } else {
  232. _capacity += length;
  233. }
  234. }
  235. void NativeByteBuffer::writeBytes(ByteArray *b, bool *error) {
  236. if (!calculateSizeOnly) {
  237. if (_position + b->length > _limit) {
  238. if (error != nullptr) {
  239. *error = true;
  240. }
  241. if (LOGS_ENABLED) DEBUG_E("write bytes error");
  242. return;
  243. }
  244. writeBytesInternal(b->bytes, 0, b->length);
  245. } else {
  246. _capacity += b->length;
  247. }
  248. }
  249. void NativeByteBuffer::writeBytesInternal(uint8_t *b, uint32_t offset, uint32_t length) {
  250. memcpy(buffer + _position, b + offset, sizeof(uint8_t) * length);
  251. _position += length;
  252. }
  253. void NativeByteBuffer::writeByte(uint8_t i, bool *error) {
  254. if (!calculateSizeOnly) {
  255. if (_position + 1 > _limit) {
  256. if (error != nullptr) {
  257. *error = true;
  258. }
  259. if (LOGS_ENABLED) DEBUG_E("write byte error");
  260. return;
  261. }
  262. buffer[_position++] = i;
  263. } else {
  264. _capacity += 1;
  265. }
  266. }
  267. void NativeByteBuffer::writeString(std::string s, bool *error) {
  268. writeByteArray((uint8_t *) s.c_str(), (uint32_t) s.length(), error);
  269. }
  270. void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t offset, uint32_t length, bool *error) {
  271. if (length <= 253) {
  272. if (!calculateSizeOnly) {
  273. if (_position + 1 > _limit) {
  274. if (error != nullptr) {
  275. *error = true;
  276. }
  277. if (LOGS_ENABLED) DEBUG_E("write byte array error");
  278. return;
  279. }
  280. buffer[_position++] = (uint8_t) length;
  281. } else {
  282. _capacity += 1;
  283. }
  284. } else {
  285. if (!calculateSizeOnly) {
  286. if (_position + 4 > _limit) {
  287. if (error != nullptr) {
  288. *error = true;
  289. }
  290. if (LOGS_ENABLED) DEBUG_E("write byte array error");
  291. return;
  292. }
  293. buffer[_position++] = (uint8_t) 254;
  294. buffer[_position++] = (uint8_t) length;
  295. buffer[_position++] = (uint8_t) (length >> 8);
  296. buffer[_position++] = (uint8_t) (length >> 16);
  297. } else {
  298. _capacity += 4;
  299. }
  300. }
  301. if (!calculateSizeOnly) {
  302. if (_position + length > _limit) {
  303. if (error != nullptr) {
  304. *error = true;
  305. }
  306. if (LOGS_ENABLED) DEBUG_E("write byte array error");
  307. return;
  308. }
  309. writeBytesInternal(b, offset, length);
  310. } else {
  311. _capacity += length;
  312. }
  313. uint32_t addition = (length + (length <= 253 ? 1 : 4)) % 4;
  314. if (addition != 0) {
  315. addition = 4 - addition;
  316. }
  317. if (!calculateSizeOnly && _position + addition > _limit) {
  318. if (error != nullptr) {
  319. *error = true;
  320. }
  321. if (LOGS_ENABLED) DEBUG_E("write byte array error");
  322. return;
  323. }
  324. for (uint32_t a = 0; a < addition; a++) {
  325. if (!calculateSizeOnly) {
  326. buffer[_position++] = (uint8_t) 0;
  327. } else {
  328. _capacity += 1;
  329. }
  330. }
  331. }
  332. void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t length, bool *error) {
  333. writeByteArray(b, 0, length, error);
  334. }
  335. void NativeByteBuffer::writeByteArray(NativeByteBuffer *b, bool *error) {
  336. b->rewind();
  337. writeByteArray(b->buffer, 0, b->limit(), error);
  338. }
  339. void NativeByteBuffer::writeByteArray(ByteArray *b, bool *error) {
  340. writeByteArray(b->bytes, 0, b->length, error);
  341. }
  342. void NativeByteBuffer::writeDouble(double d, bool *error) {
  343. int64_t value;
  344. memcpy(&value, &d, sizeof(int64_t));
  345. writeInt64(value, error);
  346. }
  347. void NativeByteBuffer::writeInt32(int32_t x) {
  348. writeInt32(x, nullptr);
  349. }
  350. void NativeByteBuffer::writeInt64(int64_t x) {
  351. writeInt64(x, nullptr);
  352. }
  353. void NativeByteBuffer::writeBool(bool value) {
  354. writeBool(value, nullptr);
  355. }
  356. void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t length) {
  357. writeBytes(b, length, nullptr);
  358. }
  359. void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t offset, uint32_t length) {
  360. writeBytes(b, offset, length, nullptr);
  361. }
  362. void NativeByteBuffer::writeBytes(ByteArray *b) {
  363. writeBytes(b, nullptr);
  364. }
  365. void NativeByteBuffer::writeBytes(NativeByteBuffer *b) {
  366. writeBytes(b, nullptr);
  367. }
  368. void NativeByteBuffer::writeByte(uint8_t i) {
  369. writeByte(i, nullptr);
  370. }
  371. void NativeByteBuffer::writeString(std::string s) {
  372. writeString(s, nullptr);
  373. }
  374. void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t offset, uint32_t length) {
  375. writeByteArray(b, offset, length, nullptr);
  376. }
  377. void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t length) {
  378. writeByteArray(b, length, nullptr);
  379. }
  380. void NativeByteBuffer::writeByteArray(NativeByteBuffer *b) {
  381. writeByteArray(b, nullptr);
  382. }
  383. void NativeByteBuffer::writeByteArray(ByteArray *b) {
  384. writeByteArray(b->bytes, b->length, nullptr);
  385. }
  386. void NativeByteBuffer::writeDouble(double d) {
  387. writeDouble(d, nullptr);
  388. }
  389. int32_t NativeByteBuffer::readInt32(bool *error) {
  390. if (_position + 4 > _limit) {
  391. if (error != nullptr) {
  392. *error = true;
  393. }
  394. if (LOGS_ENABLED) DEBUG_E("read int32 error");
  395. return 0;
  396. }
  397. int32_t result = ((buffer[_position] & 0xff)) |
  398. ((buffer[_position + 1] & 0xff) << 8) |
  399. ((buffer[_position + 2] & 0xff) << 16) |
  400. ((buffer[_position + 3] & 0xff) << 24);
  401. _position += 4;
  402. return result;
  403. }
  404. uint32_t NativeByteBuffer::readUint32(bool *error) {
  405. return (uint32_t) readInt32(error);
  406. }
  407. uint64_t NativeByteBuffer::readUint64(bool *error) {
  408. return (uint64_t) readInt64(error);
  409. }
  410. int32_t NativeByteBuffer::readBigInt32(bool *error) {
  411. if (_position + 4 > _limit) {
  412. if (error != nullptr) {
  413. *error = true;
  414. }
  415. if (LOGS_ENABLED) DEBUG_E("read big int32 error");
  416. return 0;
  417. }
  418. int32_t result = ((buffer[_position] & 0xff) << 24) |
  419. ((buffer[_position + 1] & 0xff) << 16) |
  420. ((buffer[_position + 2] & 0xff) << 8) |
  421. ((buffer[_position + 3] & 0xff));
  422. _position += 4;
  423. return result;
  424. }
  425. int64_t NativeByteBuffer::readInt64(bool *error) {
  426. if (_position + 8 > _limit) {
  427. if (error != nullptr) {
  428. *error = true;
  429. }
  430. if (LOGS_ENABLED) DEBUG_E("read int64 error");
  431. return 0;
  432. }
  433. int64_t result = ((int64_t) (buffer[_position] & 0xff)) |
  434. ((int64_t) (buffer[_position + 1] & 0xff) << 8) |
  435. ((int64_t) (buffer[_position + 2] & 0xff) << 16) |
  436. ((int64_t) (buffer[_position + 3] & 0xff) << 24) |
  437. ((int64_t) (buffer[_position + 4] & 0xff) << 32) |
  438. ((int64_t) (buffer[_position + 5] & 0xff) << 40) |
  439. ((int64_t) (buffer[_position + 6] & 0xff) << 48) |
  440. ((int64_t) (buffer[_position + 7] & 0xff) << 56);
  441. _position += 8;
  442. return result;
  443. }
  444. uint8_t NativeByteBuffer::readByte(bool *error) {
  445. if (_position + 1 > _limit) {
  446. if (error != nullptr) {
  447. *error = true;
  448. }
  449. if (LOGS_ENABLED) DEBUG_E("read byte error");
  450. return 0;
  451. }
  452. return buffer[_position++];
  453. }
  454. bool NativeByteBuffer::readBool(bool *error) {
  455. uint32_t consructor = readUint32(error);
  456. if (consructor == 0x997275b5) {
  457. return true;
  458. } else if (consructor == 0xbc799737) {
  459. return false;
  460. }
  461. if (error != nullptr) {
  462. *error = true;
  463. if (LOGS_ENABLED) DEBUG_E("read bool error");
  464. }
  465. return false;
  466. }
  467. void NativeByteBuffer::readBytes(uint8_t *b, uint32_t length, bool *error) {
  468. if (length > _limit - _position) {
  469. if (error != nullptr) {
  470. *error = true;
  471. }
  472. if (LOGS_ENABLED) DEBUG_E("read bytes error");
  473. return;
  474. }
  475. memcpy(b, buffer + _position, length);
  476. _position += length;
  477. }
  478. ByteArray *NativeByteBuffer::readBytes(uint32_t length, bool *error) {
  479. if (length > _limit - _position) {
  480. if (error != nullptr) {
  481. *error = true;
  482. }
  483. if (LOGS_ENABLED) DEBUG_E("read bytes error");
  484. return nullptr;
  485. }
  486. ByteArray *byteArray = new ByteArray(length);
  487. memcpy(byteArray->bytes, buffer + _position, sizeof(uint8_t) * length);
  488. _position += length;
  489. return byteArray;
  490. }
  491. std::string NativeByteBuffer::readString(bool *error) {
  492. uint32_t sl = 1;
  493. if (_position + 1 > _limit) {
  494. if (error != nullptr) {
  495. *error = true;
  496. }
  497. if (LOGS_ENABLED) DEBUG_E("read string error");
  498. return std::string("");
  499. }
  500. uint32_t l = buffer[_position++];
  501. if (l >= 254) {
  502. if (_position + 3 > _limit) {
  503. if (error != nullptr) {
  504. *error = true;
  505. }
  506. if (LOGS_ENABLED) DEBUG_E("read string error");
  507. return std::string("");
  508. }
  509. l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16);
  510. _position += 3;
  511. sl = 4;
  512. }
  513. uint32_t addition = (l + sl) % 4;
  514. if (addition != 0) {
  515. addition = 4 - addition;
  516. }
  517. if (_position + l + addition > _limit) {
  518. if (error != nullptr) {
  519. *error = true;
  520. }
  521. if (LOGS_ENABLED) DEBUG_E("read string error");
  522. return std::string("");
  523. }
  524. std::string result = std::string((const char *) (buffer + _position), l);
  525. _position += l + addition;
  526. return result;
  527. }
  528. ByteArray *NativeByteBuffer::readByteArray(bool *error) {
  529. uint32_t sl = 1;
  530. if (_position + 1 > _limit) {
  531. if (error != nullptr) {
  532. *error = true;
  533. }
  534. if (LOGS_ENABLED) DEBUG_E("read byte array error");
  535. return nullptr;
  536. }
  537. uint32_t l = buffer[_position++];
  538. if (l >= 254) {
  539. if (_position + 3 > _limit) {
  540. if (error != nullptr) {
  541. *error = true;
  542. }
  543. if (LOGS_ENABLED) DEBUG_E("read byte array error");
  544. return nullptr;
  545. }
  546. l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16);
  547. _position += 3;
  548. sl = 4;
  549. }
  550. uint32_t addition = (l + sl) % 4;
  551. if (addition != 0) {
  552. addition = 4 - addition;
  553. }
  554. if (_position + l + addition > _limit) {
  555. if (error != nullptr) {
  556. *error = true;
  557. }
  558. if (LOGS_ENABLED) DEBUG_E("read byte array error");
  559. return nullptr;
  560. }
  561. ByteArray *result = new ByteArray(l);
  562. memcpy(result->bytes, buffer + _position, sizeof(uint8_t) * l);
  563. _position += l + addition;
  564. return result;
  565. }
  566. NativeByteBuffer *NativeByteBuffer::readByteBuffer(bool copy, bool *error) {
  567. uint32_t sl = 1;
  568. if (_position + 1 > _limit) {
  569. if (error != nullptr) {
  570. *error = true;
  571. }
  572. if (LOGS_ENABLED) DEBUG_E("read byte buffer error");
  573. return nullptr;
  574. }
  575. uint32_t l = buffer[_position++];
  576. if (l >= 254) {
  577. if (_position + 3 > _limit) {
  578. if (error != nullptr) {
  579. *error = true;
  580. }
  581. if (LOGS_ENABLED) DEBUG_E("read byte buffer error");
  582. return nullptr;
  583. }
  584. l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16);
  585. _position += 3;
  586. sl = 4;
  587. }
  588. uint32_t addition = (l + sl) % 4;
  589. if (addition != 0) {
  590. addition = 4 - addition;
  591. }
  592. if (_position + l + addition > _limit) {
  593. if (error != nullptr) {
  594. *error = true;
  595. }
  596. if (LOGS_ENABLED) DEBUG_E("read byte buffer error");
  597. return nullptr;
  598. }
  599. NativeByteBuffer *result = nullptr;
  600. if (copy) {
  601. result = BuffersStorage::getInstance().getFreeBuffer(l);
  602. memcpy(result->buffer, buffer + _position, sizeof(uint8_t) * l);
  603. } else {
  604. result = new NativeByteBuffer(buffer + _position, l);
  605. }
  606. _position += l + addition;
  607. return result;
  608. }
  609. double NativeByteBuffer::readDouble(bool *error) {
  610. double value;
  611. int64_t value2 = readInt64(error);
  612. memcpy(&value, &value2, sizeof(double));
  613. return value;
  614. }
  615. void NativeByteBuffer::reuse() {
  616. if (sliced) {
  617. return;
  618. }
  619. BuffersStorage::getInstance().reuseFreeBuffer(this);
  620. }
  621. #ifdef ANDROID
  622. jobject NativeByteBuffer::getJavaByteBuffer() {
  623. if (javaByteBuffer == nullptr && javaVm != nullptr) {
  624. JNIEnv *env = 0;
  625. if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  626. if (LOGS_ENABLED) DEBUG_E("can't get jnienv");
  627. exit(1);
  628. }
  629. javaByteBuffer = env->NewDirectByteBuffer(buffer, _capacity);
  630. if (javaByteBuffer == nullptr) {
  631. if (LOGS_ENABLED) DEBUG_E("can't allocate NativeByteBuffer buffer");
  632. exit(1);
  633. }
  634. jobject globalRef = env->NewGlobalRef(javaByteBuffer);
  635. env->DeleteLocalRef(javaByteBuffer);
  636. javaByteBuffer = globalRef;
  637. }
  638. return javaByteBuffer;
  639. }
  640. #endif