2026-03-01 12:16:08 +08:00
# include "stdafx.h"
# include "ByteArrayOutputStream.h"
// Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
ByteArrayOutputStream : : ByteArrayOutputStream ( )
{
2026-03-09 06:53:08 -05:00
count = 0 ;
buf = byteArray ( 32 ) ;
2026-03-01 12:16:08 +08:00
}
2026-03-09 06:53:08 -05:00
// Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
// Parameters:
// size - the initial size.
2026-03-01 12:16:08 +08:00
ByteArrayOutputStream : : ByteArrayOutputStream ( unsigned int size )
{
2026-03-09 06:53:08 -05:00
count = 0 ;
buf = byteArray ( size ) ;
2026-03-01 12:16:08 +08:00
}
ByteArrayOutputStream : : ~ ByteArrayOutputStream ( )
{
2026-03-09 06:53:08 -05:00
if ( buf . data ! = NULL )
{
delete [ ] buf . data ;
}
2026-03-01 12:16:08 +08:00
}
2026-03-09 06:53:08 -05:00
// Writes the specified byte to this byte array output stream.
// Parameters:
// b - the byte to be written.
2026-03-01 12:16:08 +08:00
void ByteArrayOutputStream : : write ( unsigned int b )
{
2026-03-09 06:53:08 -05:00
// If we will fill the buffer we need to make it bigger
if ( count + 1 > = buf . length )
{
buf . resize ( buf . length * 2 ) ;
}
2026-03-01 12:16:08 +08:00
2026-03-09 06:53:08 -05:00
buf [ count ] = ( byte ) b ;
count + + ;
2026-03-01 12:16:08 +08:00
}
// Writes b.length bytes from the specified byte array to this output stream.
2026-03-09 06:53:08 -05:00
// The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).
2026-03-01 12:16:08 +08:00
void ByteArrayOutputStream : : write ( byteArray b )
{
2026-03-09 06:53:08 -05:00
write ( b , 0 , b . length ) ;
2026-03-01 12:16:08 +08:00
}
2026-03-09 06:53:08 -05:00
// Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
// Parameters:
// b - the data.
// off - the start offset in the data.
// len - the number of bytes to write.
2026-03-01 12:16:08 +08:00
void ByteArrayOutputStream : : write ( byteArray b , unsigned int offset , unsigned int length )
{
2026-03-09 06:53:08 -05:00
if ( offset > b . length | | length > b . length - offset )
{
return ;
}
2026-03-01 12:16:08 +08:00
2026-03-09 06:53:08 -05:00
if ( length > 0xFFFFFFFF - count )
{
return ;
}
2026-03-01 12:16:08 +08:00
2026-03-09 06:53:08 -05:00
// If we will fill the buffer we need to make it bigger
if ( count + length > = buf . length )
{
unsigned int newSize = ( std : : max ) ( count + length + 1 , buf . length * 2 ) ;
if ( newSize < = buf . length )
{
return ;
}
buf . resize ( newSize ) ;
}
XMemCpy ( & buf [ count ] , & b [ offset ] , length ) ;
count + = length ;
2026-03-01 12:16:08 +08:00
}
2026-03-09 06:53:08 -05:00
// Closing a ByteArrayOutputStream has no effect.
// The methods in this class can be called after the stream has been closed without generating an IOException.
2026-03-01 12:16:08 +08:00
void ByteArrayOutputStream : : close ( )
{
}
2026-03-09 06:53:08 -05:00
// 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:
// the current contents of this output stream, as a byte array.
2026-03-01 12:16:08 +08:00
byteArray ByteArrayOutputStream : : toByteArray ( )
{
2026-03-09 06:53:08 -05:00
byteArray out ( count ) ;
memcpy ( out . data , buf . data , count ) ;
return out ;
}