Update to LCEMP's ByteArrayIO version

Fixes compilation since ours was missing some revisions from LCEMP
This commit is contained in:
Loki Rautio
2026-03-09 04:49:47 -05:00
parent 164f034b0a
commit b1277c0e1b
2 changed files with 131 additions and 115 deletions

View File

@@ -2,13 +2,13 @@
#include "InputOutputStream.h" #include "InputOutputStream.h"
//Creates ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and // Creates ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and
//the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. // the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied.
//The buffer's mark is set to the specified offset. // The buffer's mark is set to the specified offset.
//Parameters: // Parameters:
//buf - the input buffer. // buf - the input buffer.
//offset - the offset in the buffer of the first byte to read. // offset - the offset in the buffer of the first byte to read.
//length - the maximum number of bytes to read from the buffer. // length - the maximum number of bytes to read from the buffer.
ByteArrayInputStream::ByteArrayInputStream(byteArray buf, unsigned int offset, unsigned int length) ByteArrayInputStream::ByteArrayInputStream(byteArray buf, unsigned int offset, unsigned int length)
: pos(offset), mark(offset) : pos(offset), mark(offset)
{ {
@@ -23,107 +23,120 @@ ByteArrayInputStream::ByteArrayInputStream(byteArray buf, unsigned int offset, u
else else
{ {
count = offset + length; count = offset + length;
this->buf = buf; }
this->buf = buf;
} }
//Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. // Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied.
//The initial value of pos is 0 and the initial value of count is the length of buf. // The initial value of pos is 0 and the initial value of count is the length of buf.
//Parameters: // Parameters:
//buf - the input buffer. // buf - the input buffer.
ByteArrayInputStream::ByteArrayInputStream(byteArray buf) ByteArrayInputStream::ByteArrayInputStream(byteArray buf)
: pos( 0 ), count( buf.length ), mark( 0 ) : pos(0), count(buf.length), mark(0)
{ {
this->buf = buf; this->buf = buf;
} }
//Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. // Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255.
//If no byte is available because the end of the stream has been reached, the value -1 is returned. // If no byte is available because the end of the stream has been reached, the value -1 is returned.
//This read method cannot block. // This read method cannot block.
//Returns: // Returns:
//the next byte of data, or -1 if the end of the stream has been reached. // the next byte of data, or -1 if the end of the stream has been reached.
int ByteArrayInputStream::read() int ByteArrayInputStream::read()
{ {
if( pos >= count ) if (pos >= count)
return -1; {
else return -1;
return buf[pos++]; }
else
{
return buf[pos++];
}
} }
//Reads some number of bytes from the input stream and stores them into the buffer array b. // Reads some number of bytes from the input stream and stores them into the buffer array b.
//The number of bytes actually read is returned as an integer. This method blocks until input data is available, // The number of bytes actually read is returned as an integer. This method blocks until input data is available,
//end of file is detected, or an exception is thrown. // end of file is detected, or an exception is thrown.
//If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. // If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte.
//If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, // If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise,
//at least one byte is read and stored into b. // at least one byte is read and stored into b.
// //
//The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, // The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is,
//at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], // at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1],
//leaving elements b[k] through b[b.length-1] unaffected. // leaving elements b[k] through b[b.length-1] unaffected.
// //
//The read(b) method for class InputStream has the same effect as: // The read(b) method for class InputStream has the same effect as:
// //
// read(b, 0, b.length) // read(b, 0, b.length)
//Parameters: // Parameters:
//b - the buffer into which the data is read. // b - the buffer into which the data is read.
//Returns: // Returns:
//the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached. // the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
int ByteArrayInputStream::read(byteArray b) int ByteArrayInputStream::read(byteArray b)
{ {
return read( b, 0, b.length ); return read(b, 0, b.length);
} }
//Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, // Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count,
//then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. // then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos.
//If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner // If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner
//performed by System.arraycopy. The value k is added into pos and k is returned. // performed by System.arraycopy. The value k is added into pos and k is returned.
//This read method cannot block. // This read method cannot block.
//Parameters: // Parameters:
//b - the buffer into which the data is read. // b - the buffer into which the data is read.
//off - the start offset in the destination array b // off - the start offset in the destination array b
//len - the maximum number of bytes read. // len - the maximum number of bytes read.
//Returns: // Returns:
//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. // the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
int ByteArrayInputStream::read(byteArray b, unsigned int offset, unsigned int length) int ByteArrayInputStream::read(byteArray b, unsigned int offset, unsigned int length)
{ {
if( pos == count ) if (pos == count)
return -1; {
return -1;
}
int k = min( length, count-pos ); int k = min(length, count - pos);
XMemCpy( &b[offset], &buf[pos], k ); XMemCpy(&b[offset], &buf[pos], k);
//std::copy( buf->data+pos, buf->data+pos+k, b->data + offset ); // Or this instead? // std::copy( buf->data+pos, buf->data+pos+k, b->data + offset ); // Or this instead?
pos += k; pos += k;
return k; return k;
} }
//Closing a ByteArrayInputStream has no effect. // Closing a ByteArrayInputStream has no effect.
//The methods in this class can be called after the stream has been closed without generating an IOException. // The methods in this class can be called after the stream has been closed without generating an IOException.
void ByteArrayInputStream::close() void ByteArrayInputStream::close()
{ {
return; return;
} }
//Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned. // Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned.
//Overrides: // Overrides:
//skip in class InputStream // skip in class InputStream
//Parameters: // Parameters:
//n - the number of bytes to be skipped. // n - the number of bytes to be skipped.
//Returns: // Returns:
//the actual number of bytes skipped. // the actual number of bytes skipped.
int64_t ByteArrayInputStream::skip(int64_t n) __int64 ByteArrayInputStream::skip(__int64 n)
{ {
int newPos = pos + n; int newPos = pos + n;
if(newPos > count) newPos = count; if (newPos > count)
{
newPos = count;
}
int k = newPos - pos; int k = newPos - pos;
pos = newPos; pos = newPos;
return k; return k;
} }
ByteArrayInputStream::~ByteArrayInputStream() ByteArrayInputStream::~ByteArrayInputStream()
{ {
if(buf.data != nullptr) delete [] buf.data; if (buf.data != NULL)
} {
delete[] buf.data;
}
}

View File

@@ -5,55 +5,58 @@
// Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. // Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
ByteArrayOutputStream::ByteArrayOutputStream() ByteArrayOutputStream::ByteArrayOutputStream()
{ {
count = 0; count = 0;
buf = byteArray( 32 ); buf = byteArray(32);
} }
//Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. // Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
//Parameters: // Parameters:
//size - the initial size. // size - the initial size.
ByteArrayOutputStream::ByteArrayOutputStream(unsigned int size) ByteArrayOutputStream::ByteArrayOutputStream(unsigned int size)
{ {
count = 0; count = 0;
buf = byteArray( size ); buf = byteArray(size);
} }
ByteArrayOutputStream::~ByteArrayOutputStream() ByteArrayOutputStream::~ByteArrayOutputStream()
{ {
if (buf.data != nullptr) if (buf.data != NULL)
delete[] buf.data; {
delete[] buf.data;
}
} }
//Writes the specified byte to this byte array output stream. // Writes the specified byte to this byte array output stream.
//Parameters: // Parameters:
//b - the byte to be written. // b - the byte to be written.
void ByteArrayOutputStream::write(unsigned int b) void ByteArrayOutputStream::write(unsigned int b)
{ {
// If we will fill the buffer we need to make it bigger // If we will fill the buffer we need to make it bigger
if( count + 1 >= buf.length ) if (count + 1 >= buf.length)
buf.resize( buf.length * 2 ); {
buf.resize(buf.length * 2);
}
buf[count] = static_cast<byte>(b); buf[count] = (byte)b;
count++; count++;
} }
// Writes b.length bytes from the specified byte array to this output stream. // Writes b.length bytes from the specified byte array to this output stream.
//The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). // The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).
void ByteArrayOutputStream::write(byteArray b) void ByteArrayOutputStream::write(byteArray b)
{ {
write(b, 0, b.length); write(b, 0, b.length);
} }
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream. // Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
//Parameters: // Parameters:
//b - the data. // b - the data.
//off - the start offset in the data. // off - the start offset in the data.
//len - the number of bytes to write. // len - the number of bytes to write.
void ByteArrayOutputStream::write(byteArray b, unsigned int offset, unsigned int length) void ByteArrayOutputStream::write(byteArray b, unsigned int offset, unsigned int length)
{ {
assert( b.length >= offset + length );
if (offset > b.length || length > b.length - offset) if (offset > b.length || length > b.length - offset)
{ {
return; return;
} }
@@ -61,9 +64,10 @@ void ByteArrayOutputStream::write(byteArray b, unsigned int offset, unsigned int
if (length > 0xFFFFFFFF - count) if (length > 0xFFFFFFFF - count)
{ {
return; return;
}
// If we will fill the buffer we need to make it bigger // If we will fill the buffer we need to make it bigger
if( count + length >= buf.length ) if (count + length >= buf.length)
{ {
unsigned int newSize = (std::max)(count + length + 1, buf.length * 2); unsigned int newSize = (std::max)(count + length + 1, buf.length * 2);
if (newSize <= buf.length) if (newSize <= buf.length)
@@ -73,24 +77,23 @@ void ByteArrayOutputStream::write(byteArray b, unsigned int offset, unsigned int
buf.resize(newSize); buf.resize(newSize);
} }
XMemCpy( &buf[count], &b[offset], length ); XMemCpy(&buf[count], &b[offset], length);
//std::copy( b->data+offset, b->data+offset+length, buf->data + count ); // Or this instead?
count += length; count += length;
} }
//Closing a ByteArrayOutputStream has no effect. // Closing a ByteArrayOutputStream has no effect.
//The methods in this class can be called after the stream has been closed without generating an IOException. // The methods in this class can be called after the stream has been closed without generating an IOException.
void ByteArrayOutputStream::close() void ByteArrayOutputStream::close()
{ {
} }
//Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it. // Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
//Returns: // Returns:
//the current contents of this output stream, as a byte array. // the current contents of this output stream, as a byte array.
byteArray ByteArrayOutputStream::toByteArray() byteArray ByteArrayOutputStream::toByteArray()
{ {
byteArray out(count); byteArray out(count);
memcpy(out.data,buf.data,count); memcpy(out.data, buf.data, count);
return out; return out;
} }