Loading...
Searching...
No Matches
juce_InputStream.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
int64 InputStream::getNumBytesRemaining()
{
auto len = getTotalLength();
if (len >= 0)
len -= getPosition();
return len;
}
ssize_t InputStream::read (void* destBuffer, size_t size)
{
ssize_t totalRead = 0;
while (size > 0)
{
auto numToRead = (int) std::min (size, (size_t) 0x70000000);
auto numRead = read (juce::addBytesToPointer (destBuffer, totalRead), numToRead);
jassert (numRead <= numToRead);
if (numRead < 0) return (ssize_t) numRead;
if (numRead == 0) break;
size -= (size_t) numRead;
totalRead += numRead;
}
return totalRead;
}
char InputStream::readByte()
{
char temp = 0;
read (&temp, 1);
return temp;
}
bool InputStream::readBool()
{
return readByte() != 0;
}
short InputStream::readShort()
{
char temp[2];
if (read (temp, 2) == 2)
return (short) ByteOrder::littleEndianShort (temp);
return 0;
}
short InputStream::readShortBigEndian()
{
char temp[2];
if (read (temp, 2) == 2)
return (short) ByteOrder::bigEndianShort (temp);
return 0;
}
int InputStream::readInt()
{
char temp[4];
if (read (temp, 4) == 4)
return (int) ByteOrder::littleEndianInt (temp);
return 0;
}
int InputStream::readIntBigEndian()
{
char temp[4];
if (read (temp, 4) == 4)
return (int) ByteOrder::bigEndianInt (temp);
return 0;
}
int InputStream::readCompressedInt()
{
auto sizeByte = (uint8) readByte();
if (sizeByte == 0)
return 0;
const int numBytes = (sizeByte & 0x7f);
if (numBytes > 4)
{
jassertfalse; // trying to read corrupt data - this method must only be used
// to read data that was written by OutputStream::writeCompressedInt()
return 0;
}
char bytes[4] = {};
if (read (bytes, numBytes) != numBytes)
return 0;
auto num = (int) ByteOrder::littleEndianInt (bytes);
return (sizeByte >> 7) ? -num : num;
}
int64 InputStream::readInt64()
{
union { uint8 asBytes[8]; uint64 asInt64; } n;
if (read (n.asBytes, 8) == 8)
return (int64) ByteOrder::swapIfBigEndian (n.asInt64);
return 0;
}
int64 InputStream::readInt64BigEndian()
{
union { uint8 asBytes[8]; uint64 asInt64; } n;
if (read (n.asBytes, 8) == 8)
return (int64) ByteOrder::swapIfLittleEndian (n.asInt64);
return 0;
}
float InputStream::readFloat()
{
static_assert (sizeof (int32) == sizeof (float), "Union assumes float has the same size as an int32");
union { int32 asInt; float asFloat; } n;
n.asInt = (int32) readInt();
return n.asFloat;
}
float InputStream::readFloatBigEndian()
{
union { int32 asInt; float asFloat; } n;
n.asInt = (int32) readIntBigEndian();
return n.asFloat;
}
double InputStream::readDouble()
{
union { int64 asInt; double asDouble; } n;
n.asInt = readInt64();
return n.asDouble;
}
double InputStream::readDoubleBigEndian()
{
union { int64 asInt; double asDouble; } n;
n.asInt = readInt64BigEndian();
return n.asDouble;
}
String InputStream::readString()
{
MemoryOutputStream buffer;
for (;;)
{
auto c = readByte();
buffer.writeByte (c);
if (c == 0)
return buffer.toUTF8();
}
}
String InputStream::readNextLine()
{
MemoryOutputStream buffer;
for (;;)
{
auto c = readByte();
if (c == 0 || c == '\n')
break;
if (c == '\r')
{
auto lastPos = getPosition();
if (readByte() != '\n')
setPosition (lastPos);
break;
}
buffer.writeByte (c);
}
return buffer.toUTF8();
}
size_t InputStream::readIntoMemoryBlock (MemoryBlock& block, ssize_t numBytes)
{
MemoryOutputStream mo (block, true);
return (size_t) mo.writeFromInputStream (*this, numBytes);
}
String InputStream::readEntireStreamAsString()
{
MemoryOutputStream mo;
mo << *this;
return mo.toString();
}
//==============================================================================
void InputStream::skipNextBytes (int64 numBytesToSkip)
{
if (numBytesToSkip > 0)
{
auto skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
HeapBlock<char> temp (skipBufferSize);
while (numBytesToSkip > 0 && ! isExhausted())
numBytesToSkip -= read (temp, (int) jmin (numBytesToSkip, (int64) skipBufferSize));
}
}
} // namespace juce
juce::ByteOrder::bigEndianInt
static constexpr uint32 bigEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:207
juce::ByteOrder::littleEndianInt
static constexpr uint32 littleEndianInt(const void *bytes) noexcept
Definition juce_ByteOrder.h:199
juce::ByteOrder::swapIfLittleEndian
static Type swapIfLittleEndian(Type value) noexcept
Definition juce_ByteOrder.h:74
juce::ByteOrder::bigEndianShort
static constexpr uint16 bigEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:206
juce::ByteOrder::littleEndianShort
static constexpr uint16 littleEndianShort(const void *bytes) noexcept
Definition juce_ByteOrder.h:198
juce::ByteOrder::swapIfBigEndian
static Type swapIfBigEndian(Type value) noexcept
Definition juce_ByteOrder.h:63
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::InputStream::getPosition
virtual int64 getPosition()=0
juce::InputStream::readInt64
virtual int64 readInt64()
Definition juce_InputStream.cpp:133
juce::InputStream::readIntBigEndian
virtual int readIntBigEndian()
Definition juce_InputStream.cpp:98
juce::InputStream::readFloatBigEndian
virtual float readFloatBigEndian()
Definition juce_InputStream.cpp:161
juce::InputStream::getNumBytesRemaining
int64 getNumBytesRemaining()
Definition juce_InputStream.cpp:26
juce::InputStream::readFloat
virtual float readFloat()
Definition juce_InputStream.cpp:153
juce::InputStream::readCompressedInt
virtual int readCompressedInt()
Definition juce_InputStream.cpp:108
juce::InputStream::setPosition
virtual bool setPosition(int64 newPosition)=0
juce::InputStream::isExhausted
virtual bool isExhausted()=0
juce::InputStream::getTotalLength
virtual int64 getTotalLength()=0
juce::InputStream::readDoubleBigEndian
virtual double readDoubleBigEndian()
Definition juce_InputStream.cpp:175
juce::InputStream::readShort
virtual short readShort()
Definition juce_InputStream.cpp:68
juce::InputStream::readInt64BigEndian
virtual int64 readInt64BigEndian()
Definition juce_InputStream.cpp:143
juce::InputStream::skipNextBytes
virtual void skipNextBytes(int64 numBytesToSkip)
Definition juce_InputStream.cpp:237
juce::InputStream::readBool
virtual bool readBool()
Definition juce_InputStream.cpp:63
juce::InputStream::readShortBigEndian
virtual short readShortBigEndian()
Definition juce_InputStream.cpp:78
juce::InputStream::readIntoMemoryBlock
virtual size_t readIntoMemoryBlock(MemoryBlock &destBlock, ssize_t maxNumBytesToRead=-1)
Definition juce_InputStream.cpp:223
juce::InputStream::read
virtual int read(void *destBuffer, int maxBytesToRead)=0
juce::InputStream::readNextLine
virtual String readNextLine()
Definition juce_InputStream.cpp:196
juce::InputStream::readEntireStreamAsString
virtual String readEntireStreamAsString()
Definition juce_InputStream.cpp:229
juce::InputStream::readInt
virtual int readInt()
Definition juce_InputStream.cpp:88
juce::InputStream::readDouble
virtual double readDouble()
Definition juce_InputStream.cpp:168
juce::InputStream::readString
virtual String readString()
Definition juce_InputStream.cpp:182
juce::InputStream::readByte
virtual char readByte()
Definition juce_InputStream.cpp:56
juce::MemoryBlock
Definition juce_MemoryBlock.h:33
juce::MemoryOutputStream
Definition juce_MemoryOutputStream.h:36
juce::MemoryOutputStream::toUTF8
String toUTF8() const
Definition juce_MemoryOutputStream.cpp:189
juce::MemoryOutputStream::toString
String toString() const
Definition juce_MemoryOutputStream.cpp:195
juce::MemoryOutputStream::writeFromInputStream
int64 writeFromInputStream(InputStream &, int64 maxNumBytesToWrite) override
Definition juce_MemoryOutputStream.cpp:172
juce::OutputStream::writeByte
virtual bool writeByte(char byte)
Definition juce_OutputStream.cpp:83
juce::String
Definition juce_String.h:53