juce_InputStream.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. int64 InputStream::getNumBytesRemaining()
  22. {
  23. int64 len = getTotalLength();
  24. if (len >= 0)
  25. len -= getPosition();
  26. return len;
  27. }
  28. char InputStream::readByte()
  29. {
  30. char temp = 0;
  31. read (&temp, 1);
  32. return temp;
  33. }
  34. bool InputStream::readBool()
  35. {
  36. return readByte() != 0;
  37. }
  38. short InputStream::readShort()
  39. {
  40. char temp[2];
  41. if (read (temp, 2) == 2)
  42. return (short) ByteOrder::littleEndianShort (temp);
  43. return 0;
  44. }
  45. short InputStream::readShortBigEndian()
  46. {
  47. char temp[2];
  48. if (read (temp, 2) == 2)
  49. return (short) ByteOrder::bigEndianShort (temp);
  50. return 0;
  51. }
  52. int InputStream::readInt()
  53. {
  54. char temp[4];
  55. if (read (temp, 4) == 4)
  56. return (int) ByteOrder::littleEndianInt (temp);
  57. return 0;
  58. }
  59. int InputStream::readIntBigEndian()
  60. {
  61. char temp[4];
  62. if (read (temp, 4) == 4)
  63. return (int) ByteOrder::bigEndianInt (temp);
  64. return 0;
  65. }
  66. int InputStream::readCompressedInt()
  67. {
  68. const uint8 sizeByte = (uint8) readByte();
  69. if (sizeByte == 0)
  70. return 0;
  71. const int numBytes = (sizeByte & 0x7f);
  72. if (numBytes > 4)
  73. {
  74. jassertfalse; // trying to read corrupt data - this method must only be used
  75. // to read data that was written by OutputStream::writeCompressedInt()
  76. return 0;
  77. }
  78. char bytes[4] = { 0, 0, 0, 0 };
  79. if (read (bytes, numBytes) != numBytes)
  80. return 0;
  81. const int num = (int) ByteOrder::littleEndianInt (bytes);
  82. return (sizeByte >> 7) ? -num : num;
  83. }
  84. int64 InputStream::readInt64()
  85. {
  86. union { uint8 asBytes[8]; uint64 asInt64; } n;
  87. if (read (n.asBytes, 8) == 8)
  88. return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
  89. return 0;
  90. }
  91. int64 InputStream::readInt64BigEndian()
  92. {
  93. union { uint8 asBytes[8]; uint64 asInt64; } n;
  94. if (read (n.asBytes, 8) == 8)
  95. return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
  96. return 0;
  97. }
  98. float InputStream::readFloat()
  99. {
  100. // the union below relies on these types being the same size...
  101. static_jassert (sizeof (int32) == sizeof (float));
  102. union { int32 asInt; float asFloat; } n;
  103. n.asInt = (int32) readInt();
  104. return n.asFloat;
  105. }
  106. float InputStream::readFloatBigEndian()
  107. {
  108. union { int32 asInt; float asFloat; } n;
  109. n.asInt = (int32) readIntBigEndian();
  110. return n.asFloat;
  111. }
  112. double InputStream::readDouble()
  113. {
  114. union { int64 asInt; double asDouble; } n;
  115. n.asInt = readInt64();
  116. return n.asDouble;
  117. }
  118. double InputStream::readDoubleBigEndian()
  119. {
  120. union { int64 asInt; double asDouble; } n;
  121. n.asInt = readInt64BigEndian();
  122. return n.asDouble;
  123. }
  124. String InputStream::readString()
  125. {
  126. MemoryBlock buffer (256);
  127. char* data = static_cast<char*> (buffer.getData());
  128. size_t i = 0;
  129. while ((data[i] = readByte()) != 0)
  130. {
  131. if (++i >= buffer.getSize())
  132. {
  133. buffer.setSize (buffer.getSize() + 512);
  134. data = static_cast<char*> (buffer.getData());
  135. }
  136. }
  137. return String::fromUTF8 (data, (int) i);
  138. }
  139. String InputStream::readNextLine()
  140. {
  141. MemoryBlock buffer (256);
  142. char* data = static_cast<char*> (buffer.getData());
  143. size_t i = 0;
  144. while ((data[i] = readByte()) != 0)
  145. {
  146. if (data[i] == '\n')
  147. break;
  148. if (data[i] == '\r')
  149. {
  150. const int64 lastPos = getPosition();
  151. if (readByte() != '\n')
  152. setPosition (lastPos);
  153. break;
  154. }
  155. if (++i >= buffer.getSize())
  156. {
  157. buffer.setSize (buffer.getSize() + 512);
  158. data = static_cast<char*> (buffer.getData());
  159. }
  160. }
  161. return String::fromUTF8 (data, (int) i);
  162. }
  163. size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
  164. {
  165. MemoryOutputStream mo (block, true);
  166. return (size_t) mo.writeFromInputStream (*this, numBytes);
  167. }
  168. String InputStream::readEntireStreamAsString()
  169. {
  170. MemoryOutputStream mo;
  171. mo << *this;
  172. return mo.toString();
  173. }
  174. //==============================================================================
  175. void InputStream::skipNextBytes (int64 numBytesToSkip)
  176. {
  177. if (numBytesToSkip > 0)
  178. {
  179. const int skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
  180. HeapBlock<char> temp ((size_t) skipBufferSize);
  181. while (numBytesToSkip > 0 && ! isExhausted())
  182. numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
  183. }
  184. }