resizeStream.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "resizeStream.h"
  23. ResizeFilterStream::ResizeFilterStream()
  24. : m_pStream(NULL),
  25. m_startOffset(0),
  26. m_streamLen(0),
  27. m_currOffset(0)
  28. {
  29. //
  30. }
  31. ResizeFilterStream::~ResizeFilterStream()
  32. {
  33. detachStream();
  34. }
  35. bool ResizeFilterStream::attachStream(Stream* io_pSlaveStream)
  36. {
  37. AssertFatal(io_pSlaveStream != NULL, "NULL Slave stream?");
  38. m_pStream = io_pSlaveStream;
  39. m_startOffset = 0;
  40. m_streamLen = m_pStream->getStreamSize();
  41. m_currOffset = 0;
  42. setStatus(EOS);
  43. return true;
  44. }
  45. void ResizeFilterStream::detachStream()
  46. {
  47. m_pStream = NULL;
  48. m_startOffset = 0;
  49. m_streamLen = 0;
  50. m_currOffset = 0;
  51. setStatus(Closed);
  52. }
  53. Stream* ResizeFilterStream::getStream()
  54. {
  55. return m_pStream;
  56. }
  57. bool ResizeFilterStream::setStreamOffset(const U32 in_startOffset, const U32 in_streamLen)
  58. {
  59. AssertFatal(m_pStream != NULL, "stream not attached!");
  60. if (m_pStream == NULL)
  61. return false;
  62. U32 start = in_startOffset;
  63. U32 end = in_startOffset + in_streamLen;
  64. U32 actual = m_pStream->getStreamSize();
  65. if (start >= actual || end > actual)
  66. return false;
  67. m_startOffset = start;
  68. m_streamLen = in_streamLen;
  69. m_currOffset = 0;
  70. if (m_streamLen != 0)
  71. setStatus(Ok);
  72. else
  73. setStatus(EOS);
  74. return true;
  75. }
  76. U32 ResizeFilterStream::getPosition() const
  77. {
  78. AssertFatal(m_pStream != NULL, "Error, stream not attached");
  79. if (m_pStream == NULL)
  80. return 0;
  81. return m_currOffset;
  82. }
  83. bool ResizeFilterStream::setPosition(const U32 in_newPosition)
  84. {
  85. AssertFatal(m_pStream != NULL, "Error, stream not attached");
  86. if (m_pStream == NULL)
  87. return false;
  88. if (in_newPosition < m_streamLen) {
  89. m_currOffset = in_newPosition;
  90. return true;
  91. } else {
  92. m_currOffset = m_streamLen;
  93. return false;
  94. }
  95. }
  96. U32 ResizeFilterStream::getStreamSize()
  97. {
  98. AssertFatal(m_pStream != NULL, "Error, stream not attached");
  99. return m_streamLen;
  100. }
  101. bool ResizeFilterStream::_read(const U32 in_numBytes, void* out_pBuffer)
  102. {
  103. AssertFatal(m_pStream != NULL, "Error, stream not attached");
  104. if (in_numBytes == 0)
  105. return true;
  106. AssertFatal(out_pBuffer != NULL, "Invalid output buffer");
  107. if (getStatus() == Closed) {
  108. AssertFatal(false, "Attempted read from closed stream");
  109. return false;
  110. }
  111. U32 savePosition = m_pStream->getPosition();
  112. if (m_pStream->setPosition(m_startOffset + m_currOffset) == false)
  113. return false;
  114. U32 actualSize = in_numBytes;
  115. U32 position = m_startOffset + m_currOffset;
  116. if (in_numBytes + position > m_startOffset + m_streamLen)
  117. actualSize = m_streamLen - (position - m_startOffset);
  118. if (actualSize == 0) {
  119. setStatus(EOS);
  120. return false;
  121. }
  122. bool success = m_pStream->read(actualSize, out_pBuffer);
  123. m_currOffset += actualSize;
  124. setStatus(m_pStream->getStatus());
  125. m_pStream->setPosition(savePosition);
  126. return success;
  127. }