refactor: remove arrayWithLength, replace with std::vector

Eliminates the custom arrayWithLength<T> wrapper and all typedefs, replacing with std::vector<T> directly.
This commit is contained in:
MatthewBeshay
2026-03-31 12:06:19 +11:00
parent 27a4964941
commit 7ddfaeb59e
414 changed files with 2412 additions and 2724 deletions

View File

@@ -4,39 +4,39 @@
class Arrays {
public:
static void fill(doubleArray arr, unsigned int from, unsigned int to,
static void fill(std::vector<double>& arr, unsigned int from, unsigned int to,
double value) {
assert(from >= 0);
assert(from <= to);
assert(to <= arr.length);
std::fill(arr.data + from, arr.data + to, value);
assert(to <= arr.size());
std::fill(arr.data() + from, arr.data() + to, value);
}
static void fill(floatArray arr, unsigned int from, unsigned int to,
static void fill(std::vector<float>& arr, unsigned int from, unsigned int to,
float value) {
assert(from >= 0);
assert(from <= to);
assert(to <= arr.length);
std::fill(arr.data + from, arr.data + to, value);
assert(to <= arr.size());
std::fill(arr.data() + from, arr.data() + to, value);
}
static void fill(BiomeArray arr, unsigned int from, unsigned int to,
static void fill(std::vector<Biome*>& arr, unsigned int from, unsigned int to,
Biome* value) {
assert(from >= 0);
assert(from <= to);
assert(to <= arr.length);
std::fill(arr.data + from, arr.data + to, value);
assert(to <= arr.size());
std::fill(arr.data() + from, arr.data() + to, value);
}
static void fill(byteArray arr, unsigned int from, unsigned int to,
static void fill(std::vector<uint8_t>& arr, unsigned int from, unsigned int to,
uint8_t value) {
assert(from >= 0);
assert(from <= to);
assert(to <= arr.length);
std::fill(arr.data + from, arr.data + to, value);
assert(to <= arr.size());
std::fill(arr.data() + from, arr.data() + to, value);
}
static void fill(byteArray arr, uint8_t value) {
std::fill(arr.data, arr.data + arr.length, value);
static void fill(std::vector<uint8_t>& arr, uint8_t value) {
std::fill(arr.data(), arr.data() + arr.size(), value);
}
};

View File

@@ -20,7 +20,7 @@ public:
ByteBuffer(unsigned int capacity, uint8_t* backingArray);
virtual ~ByteBuffer();
static ByteBuffer* wrap(byteArray& b);
static ByteBuffer* wrap(std::vector<uint8_t>& b);
static ByteBuffer* allocate(unsigned int capacity);
void order(ByteOrder a);
ByteBuffer* flip();
@@ -28,19 +28,19 @@ public:
int getSize();
int getInt();
int getInt(unsigned int index);
void get(byteArray) {} // 4J - TODO
void get(std::vector<uint8_t>) {} // 4J - TODO
uint8_t get(int index);
int64_t getLong();
short getShort();
void getShortArray(shortArray& s);
void getShortArray(std::vector<short>& s);
ByteBuffer* put(int index, uint8_t b);
ByteBuffer* putInt(int value);
ByteBuffer* putInt(unsigned int index, int value);
ByteBuffer* putShort(short value);
ByteBuffer* putShortArray(shortArray& s);
ByteBuffer* putShortArray(std::vector<short>& s);
ByteBuffer* putLong(int64_t value);
ByteBuffer* put(byteArray inputArray);
byteArray array();
ByteBuffer* put(std::vector<uint8_t>& inputArray);
std::vector<uint8_t> array();
IntBuffer* asIntBuffer();
FloatBuffer* asFloatBuffer();
};

View File

@@ -13,6 +13,6 @@ public:
FloatBuffer* flip();
FloatBuffer* put(float f);
void get(arrayWithLength<float>* dst);
void get(std::vector<float>* dst);
float* _getDataPointer() { return buffer; }
};

View File

@@ -8,7 +8,7 @@ private:
OutputStream* stream;
protected:
byteArray buf; // The internal buffer where data is stored.
std::vector<uint8_t> buf; // The internal buffer where data is stored.
unsigned int count; // The number of valid bytes in the buffer.
public:
@@ -17,7 +17,7 @@ public:
virtual void flush();
virtual void close();
virtual void write(byteArray b, unsigned int offset, unsigned int length);
virtual void write(byteArray b);
virtual void write(const std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void write(const std::vector<uint8_t>& b);
virtual void write(unsigned int b);
};

View File

@@ -6,7 +6,7 @@
class ByteArrayInputStream : public InputStream {
protected:
byteArray buf; // An array of bytes that was provided by the creator of the
std::vector<uint8_t> buf; // An array of bytes that was provided by the creator of the
// stream.
unsigned int count; // The index one greater than the last valid character
// in the input stream buffer.
@@ -15,20 +15,20 @@ protected:
// stream buffer.
public:
ByteArrayInputStream(byteArray buf, unsigned int offset,
ByteArrayInputStream(std::vector<uint8_t>& buf, unsigned int offset,
unsigned int length);
ByteArrayInputStream(byteArray buf);
ByteArrayInputStream(std::vector<uint8_t>& buf);
virtual ~ByteArrayInputStream();
virtual int read();
virtual int read(byteArray b);
virtual int read(byteArray b, unsigned int offset, unsigned int length);
virtual int read(std::vector<uint8_t>& b);
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual int64_t skip(int64_t n);
// 4J Stu Added - Sometimes we don't want to delete the data on destroying
// this
void reset() {
buf = byteArray();
buf = std::vector<uint8_t>();
count = 0;
mark = 0;
pos = 0;

View File

@@ -4,10 +4,10 @@
#include "../../../../Minecraft.World/ConsoleHelpers/ArrayWithLength.h" // 4jcraft TODO
class ByteArrayOutputStream : public OutputStream {
// Note - when actually implementing, byteArray will need to grow as data is
// Note - when actually implementing, std::vector<uint8_t> will need to grow as data is
// written
public:
byteArray buf; // The buffer where data is stored.
std::vector<uint8_t> buf; // The buffer where data is stored.
protected:
unsigned int count; // The number of valid bytes in the buffer.
@@ -19,10 +19,10 @@ public:
virtual void flush() {}
virtual void write(unsigned int b);
virtual void write(byteArray b);
virtual void write(byteArray b, unsigned int offset, unsigned int length);
virtual void write(const std::vector<uint8_t>& b);
virtual void write(const std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual byteArray toByteArray();
virtual std::vector<uint8_t> toByteArray();
void reset() { count = 0; }
unsigned int size() { return count; }

View File

@@ -5,12 +5,12 @@
class DataInput {
public:
virtual int read() = 0;
virtual int read(byteArray b) = 0;
virtual int read(byteArray b, unsigned int offset, unsigned int length) = 0;
virtual int read(std::vector<uint8_t>& b) = 0;
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length) = 0;
virtual bool readBoolean() = 0;
virtual uint8_t readByte() = 0;
virtual unsigned char readUnsignedByte() = 0;
virtual bool readFully(byteArray a) = 0;
virtual bool readFully(std::vector<uint8_t>& a) = 0;
virtual double readDouble() = 0;
virtual float readFloat() = 0;
virtual int readInt() = 0;

View File

@@ -16,15 +16,15 @@ private:
public:
DataInputStream(InputStream* in);
virtual int read();
virtual int read(byteArray b);
virtual int read(byteArray b, unsigned int offset, unsigned int length);
virtual int read(std::vector<uint8_t>& b);
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual bool readBoolean();
virtual uint8_t readByte();
virtual unsigned char readUnsignedByte();
virtual wchar_t readChar();
virtual bool readFully(byteArray b);
virtual bool readFully(charArray b);
virtual bool readFully(std::vector<uint8_t>& b);
virtual bool readFully(std::vector<char>& b);
virtual double readDouble();
virtual float readFloat();
virtual int readInt();

View File

@@ -5,8 +5,8 @@
class DataOutput {
public:
virtual void write(unsigned int b) = 0;
virtual void write(byteArray b) = 0;
virtual void write(byteArray b, unsigned int offset,
virtual void write(const std::vector<uint8_t>& b) = 0;
virtual void write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) = 0;
virtual void writeByte(uint8_t a) = 0;
virtual void writeDouble(double a) = 0;

View File

@@ -25,8 +25,8 @@ public:
void deleteChildStream();
virtual void write(unsigned int b);
virtual void write(byteArray b);
virtual void write(byteArray b, unsigned int offset, unsigned int length);
virtual void write(const std::vector<uint8_t>& b);
virtual void write(const std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual void writeByte(uint8_t a);
virtual void writeDouble(double a);

View File

@@ -15,8 +15,8 @@ public:
FileInputStream(const File& file);
virtual ~FileInputStream();
virtual int read();
virtual int read(byteArray b);
virtual int read(byteArray b, unsigned int offset, unsigned int length);
virtual int read(std::vector<uint8_t>& b);
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual int64_t skip(int64_t n);

View File

@@ -14,8 +14,8 @@ public:
FileOutputStream(const File& file);
virtual ~FileOutputStream();
virtual void write(unsigned int b);
virtual void write(byteArray b);
virtual void write(byteArray b, unsigned int offset, unsigned int length);
virtual void write(const std::vector<uint8_t>& b);
virtual void write(const std::vector<uint8_t>& b, unsigned int offset, unsigned int length);
virtual void close();
virtual void flush();

View File

@@ -15,8 +15,8 @@ private:
public:
GZIPInputStream(InputStream* out) : stream(out) {};
virtual int read() { return stream->read(); };
virtual int read(byteArray b) { return stream->read(b); };
virtual int read(byteArray b, unsigned int offset, unsigned int length) {
virtual int read(std::vector<uint8_t>& b) { return stream->read(b); };
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length) {
return stream->read(b, offset, length);
};
virtual void close() { return stream->close(); };

View File

@@ -13,8 +13,8 @@ private:
public:
GZIPOutputStream(OutputStream* out) : stream(out) {};
virtual void write(unsigned int b) { stream->write(b); };
virtual void write(byteArray b) { stream->write(b); };
virtual void write(byteArray b, unsigned int offset, unsigned int length) {
virtual void write(const std::vector<uint8_t>& b) { stream->write(b); };
virtual void write(const std::vector<uint8_t>& b, unsigned int offset, unsigned int length) {
stream->write(b, offset, length);
};
virtual void close() { stream->close(); };

View File

@@ -12,8 +12,8 @@ public:
virtual ~InputStream() {}
virtual int read() = 0;
virtual int read(byteArray b) = 0;
virtual int read(byteArray b, unsigned int offset, unsigned int length) = 0;
virtual int read(std::vector<uint8_t>& b) = 0;
virtual int read(std::vector<uint8_t>& b, unsigned int offset, unsigned int length) = 0;
virtual void close() = 0;
virtual int64_t skip(int64_t n) = 0;

View File

@@ -9,8 +9,8 @@ public:
virtual ~OutputStream() {}
virtual void write(unsigned int b) = 0;
virtual void write(byteArray b) = 0;
virtual void write(byteArray b, unsigned int offset,
virtual void write(const std::vector<uint8_t>& b) = 0;
virtual void write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) = 0;
virtual void close() = 0;
virtual void flush() = 0;

View File

@@ -16,8 +16,8 @@ public:
virtual IntBuffer* flip();
int get(unsigned int index);
int* getBuffer();
IntBuffer* put(intArray* inputArray, unsigned int offset,
IntBuffer* put(std::vector<int>* inputArray, unsigned int offset,
unsigned int length);
IntBuffer* put(intArray inputArray);
IntBuffer* put(std::vector<int>& inputArray);
IntBuffer* put(int i);
};

View File

@@ -1,24 +1,28 @@
#pragma once
#include <cstdint>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cassert>
#include "../../../Minecraft.World/ConsoleHelpers/ArrayWithLength.h" // 4jcraft TODO
// 4J Jev, just thought it would be easier this way.
#define ArrayCopyFunctionDeclaration(x) \
static void arraycopy(arrayWithLength<x> src, unsigned int srcPos, \
arrayWithLength<x>* dst, unsigned int dstPos, \
#define ArrayCopyFunctionDeclaration(x) \
static void arraycopy(const std::vector<x>& src, unsigned int srcPos, \
std::vector<x>* dst, unsigned int dstPos, \
unsigned int length);
#define ArrayCopyFunctionDefinition(x) \
void System::arraycopy(arrayWithLength<x> src, unsigned int srcPos, \
arrayWithLength<x>* dst, unsigned int dstPos, \
unsigned int length) { \
arraycopy<x>(src, srcPos, dst, dstPos, length); \
#define ArrayCopyFunctionDefinition(x) \
void System::arraycopy(const std::vector<x>& src, unsigned int srcPos, \
std::vector<x>* dst, unsigned int dstPos, \
unsigned int length) { \
arraycopy<x>(src, srcPos, dst, dstPos, length); \
}
class System {
template <class T>
static void arraycopy(arrayWithLength<T> src, unsigned int srcPos,
arrayWithLength<T>* dst, unsigned int dstPos,
static void arraycopy(const std::vector<T>& src, unsigned int srcPos,
std::vector<T>* dst, unsigned int dstPos,
unsigned int length);
public:

View File

@@ -36,7 +36,7 @@ ByteBuffer::~ByteBuffer() {
// Wraps a byte array into a buffer.
// The new buffer will be backed by the given uint8_t array; that is,
// modifications to the buffer will cause the array to be modified and vice
// versa. The new buffer's capacity and limit will be array.length, its position
// versa. The new buffer's capacity and limit will be array.size(), its position
// will be zero, and its mark will be undefined. Its backing array will be the
// given array, and its array offset will be zero.
//
@@ -44,8 +44,8 @@ ByteBuffer::~ByteBuffer() {
// array - The array that will back this buffer
// Returns:
// The new byte buffer
ByteBuffer* ByteBuffer::wrap(byteArray& b) {
return new ByteBuffer(b.length, b.data);
ByteBuffer* ByteBuffer::wrap(std::vector<uint8_t>& b) {
return new ByteBuffer(b.size(), b.data());
}
// Allocates a new byte buffer.
@@ -211,13 +211,13 @@ short ByteBuffer::getShort() {
return value;
}
void ByteBuffer::getShortArray(shortArray& s) {
void ByteBuffer::getShortArray(std::vector<short>& s) {
// TODO 4J Stu - Should this function be writing from the start of the
// buffer, or from position? And should it update position?
assert(s.length >= m_limit / 2);
assert(s.size() >= m_limit / 2);
// 4J Stu - Assumes big endian
memcpy(s.data, buffer, (m_limit - m_position));
memcpy(s.data(), buffer, (m_limit - m_position));
}
// Absolute put method (optional operation).
@@ -320,13 +320,13 @@ ByteBuffer* ByteBuffer::putShort(short value) {
return this;
}
ByteBuffer* ByteBuffer::putShortArray(shortArray& s) {
ByteBuffer* ByteBuffer::putShortArray(std::vector<short>& s) {
// TODO 4J Stu - Should this function be writing from the start of the
// buffer, or from position? And should it update position?
assert(s.length * 2 <= m_limit);
assert(s.size() * 2 <= m_limit);
// 4J Stu - Assumes big endian
memcpy(buffer, s.data, s.length * 2);
memcpy(buffer, s.data(), s.size() * 2);
return this;
}
@@ -371,22 +371,22 @@ ByteBuffer* ByteBuffer::putLong(int64_t value) {
// this buffer. An invocation of this method of the form dst.put(a) behaves in
// exactly the same way as the invocation
//
// dst.put(a, 0, a.length)
// dst.put(a, 0, a.size())
// Returns:
// This buffer
ByteBuffer* ByteBuffer::put(byteArray inputArray) {
if (inputArray.length > remaining())
ByteBuffer* ByteBuffer::put(std::vector<uint8_t>& inputArray) {
if (inputArray.size() > remaining())
assert(false); // TODO 4J Stu - Some kind of exception?
std::copy(inputArray.data, inputArray.data + inputArray.length,
std::copy(inputArray.data(), inputArray.data() + inputArray.size(),
buffer + m_position);
m_position += inputArray.length;
m_position += inputArray.size();
return this;
}
byteArray ByteBuffer::array() { return byteArray(buffer, m_capacity); }
std::vector<uint8_t> ByteBuffer::array() { return std::vector<uint8_t>(buffer, buffer + m_capacity); }
// Creates a view of this byte buffer as an int buffer.
// The content of the new buffer will start at this buffer's current position.

View File

@@ -4,8 +4,8 @@
#include "java/FileFilter.h"
#include "java/File.h"
#include "../../../Minecraft.World/ConsoleHelpers/PathHelper.h" // 4jcraft TODO
#include "../../../Minecraft.World/ConsoleHelpers/StringHelpers.h" // 4jcraft TODO
#include "../../Minecraft.World/ConsoleHelpers/PathHelper.h" // 4jcraft TODO
#include "../../Minecraft.World/ConsoleHelpers/StringHelpers.h" // 4jcraft TODO
const wchar_t File::pathSeparator = L'/';

View File

@@ -51,11 +51,11 @@ FloatBuffer* FloatBuffer::put(float f) {
// array. An invocation of this method of the form src.get(a) behaves in exactly
// the same way as the invocation
//
// src.get(a, 0, a.length)
// src.get(a, 0, a.size())
// Returns:
// This buffer
void FloatBuffer::get(floatArray* dst) {
assert(dst->length <= m_capacity);
void FloatBuffer::get(std::vector<float>* dst) {
assert(dst->size() <= m_capacity);
for (unsigned int i = 0; i < dst->length; i++) dst->data[i] = buffer[i];
for (unsigned int i = 0; i < dst->size(); i++) (*dst)[i] = buffer[i];
}

View File

@@ -5,7 +5,7 @@
// the underlying output stream. size - the buffer size.
BufferedOutputStream::BufferedOutputStream(OutputStream* out, int size) {
stream = out;
buf = byteArray(size);
buf = std::vector<uint8_t>(size);
count = 0;
}
@@ -14,7 +14,6 @@ BufferedOutputStream::~BufferedOutputStream() {
// TODO: ArrayWithLength.h doesnt have a destructor.
// this wouldnt need to be done manually.
// but for some reason the destructor is commented out in the source code?
delete[] buf.data;
}
// Flushes this buffered output stream. This forces any buffered output bytes to
@@ -61,11 +60,11 @@ void BufferedOutputStream::close() {
// b - the data.
// off - the start offset in the data.
// len - the number of bytes to write.
void BufferedOutputStream::write(byteArray b, unsigned int offset,
void BufferedOutputStream::write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
// Over the length of what we can store in our buffer - just flush the
// buffer and output directly
if (length >= buf.length) {
if (length >= buf.size()) {
flush();
stream->write(b, offset, length);
} else {
@@ -75,13 +74,13 @@ void BufferedOutputStream::write(byteArray b, unsigned int offset,
}
}
// Writes b.length bytes to this output stream.
// Writes b.size() bytes to this output stream.
// The write method of FilterOutputStream calls its write method of three
// arguments with the arguments b, 0, and b.length.
// arguments with the arguments b, 0, and b.size().
//
// Note that this method does not call the one-argument write method of its
// underlying stream with the single argument b.
void BufferedOutputStream::write(byteArray b) { write(b, 0, b.length); }
void BufferedOutputStream::write(const std::vector<uint8_t>& b) { write(b, 0, b.size()); }
// Writes the specified byte to this buffered output stream.
// Overrides:
@@ -90,7 +89,7 @@ void BufferedOutputStream::write(byteArray b) { write(b, 0, b.length); }
// b - the byte to be written.
void BufferedOutputStream::write(unsigned int b) {
buf[count++] = (uint8_t)b;
if (count == buf.length) {
if (count == buf.size()) {
flush();
}
}

View File

@@ -2,21 +2,21 @@
// 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 buffer's
// offset+length and buf.size(). The buffer array is not copied. The buffer's
// mark is set to the specified offset. Parameters: buf - the input buffer.
// offset - the offset in the buffer of the first byte to read.
// length - the maximum number of bytes to read from the buffer.
ByteArrayInputStream::ByteArrayInputStream(byteArray buf, unsigned int offset,
ByteArrayInputStream::ByteArrayInputStream(std::vector<uint8_t>& buf, unsigned int offset,
unsigned int length)
: pos(offset), count(std::min(offset + length, buf.length)), mark(offset) {
: pos(offset), count(std::min(offset + length, (unsigned int)buf.size())), mark(offset) {
this->buf = buf;
}
// 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. Parameters: buf - the input buffer.
ByteArrayInputStream::ByteArrayInputStream(byteArray buf)
: pos(0), count(buf.length), mark(0) {
ByteArrayInputStream::ByteArrayInputStream(std::vector<uint8_t>& buf)
: pos(0), count(buf.size()), mark(0) {
this->buf = buf;
}
@@ -43,17 +43,17 @@ int ByteArrayInputStream::read() {
// 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], leaving elements b[k] through b[b.length-1] unaffected.
// b[0] through b[k-1], leaving elements b[k] through b[b.size()-1] unaffected.
//
// The read(b) method for class InputStream has the same effect as:
//
// read(b, 0, b.length)
// read(b, 0, b.size())
// Parameters:
// b - the buffer into which the data is read.
// 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.
int ByteArrayInputStream::read(byteArray b) { return read(b, 0, b.length); }
int ByteArrayInputStream::read(std::vector<uint8_t>& b) { return read(b, 0, b.size()); }
// 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,
@@ -68,7 +68,7 @@ int ByteArrayInputStream::read(byteArray b) { return read(b, 0, b.length); }
// 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.
int ByteArrayInputStream::read(byteArray b, unsigned int offset,
int ByteArrayInputStream::read(std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
if (pos == count) return -1;
@@ -105,5 +105,4 @@ int64_t ByteArrayInputStream::skip(int64_t n) {
}
ByteArrayInputStream::~ByteArrayInputStream() {
if (buf.data != nullptr) delete[] buf.data;
}

View File

@@ -4,18 +4,17 @@
// bytes, though its size increases if necessary.
ByteArrayOutputStream::ByteArrayOutputStream() {
count = 0;
buf = byteArray(32);
buf = std::vector<uint8_t>(32);
}
// Creates a new byte array output stream, with a buffer capacity of the
// specified size, in bytes. Parameters: size - the initial size.
ByteArrayOutputStream::ByteArrayOutputStream(unsigned int size) {
count = 0;
buf = byteArray(size);
buf = std::vector<uint8_t>(size);
}
ByteArrayOutputStream::~ByteArrayOutputStream() {
if (buf.data != nullptr) delete[] buf.data;
}
// Writes the specified byte to this byte array output stream.
@@ -23,27 +22,27 @@ ByteArrayOutputStream::~ByteArrayOutputStream() {
// b - the byte to be written.
void ByteArrayOutputStream::write(unsigned int b) {
// If we will fill the buffer we need to make it bigger
if (count + 1 >= buf.length) buf.resize(buf.length * 2);
if (count + 1 >= buf.size()) buf.resize(buf.size() * 2);
buf[count] = (uint8_t)b;
count++;
}
// Writes b.length bytes from the specified byte array to this output stream.
// Writes b.size() 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).
void ByteArrayOutputStream::write(byteArray b) { write(b, 0, b.length); }
// effect as the call write(b, 0, b.size()).
void ByteArrayOutputStream::write(const std::vector<uint8_t>& b) { write(b, 0, b.size()); }
// 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.
void ByteArrayOutputStream::write(byteArray b, unsigned int offset,
void ByteArrayOutputStream::write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
assert(b.length >= offset + length);
assert(b.size() >= offset + length);
// If we will fill the buffer we need to make it bigger
if (count + length >= buf.length)
buf.resize(std::max(count + length + 1, buf.length * 2));
if (count + length >= buf.size())
buf.resize(std::max(count + length + 1, (unsigned int)(buf.size() * 2)));
std::memcpy(&buf[count], &b[offset], length);
// std::copy( b->data+offset, b->data+offset+length, buf->data + count ); //
@@ -61,8 +60,8 @@ void ByteArrayOutputStream::close() {}
// 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.
byteArray ByteArrayOutputStream::toByteArray() {
byteArray out(count);
memcpy(out.data, buf.data, count);
std::vector<uint8_t> ByteArrayOutputStream::toByteArray() {
std::vector<uint8_t> out(count);
memcpy(out.data(), buf.data(), count);
return out;
}

View File

@@ -36,11 +36,11 @@ int DataInputStream::read() {
// 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], leaving elements b[k] through b[b.length-1] unaffected.
// b[0] through b[k-1], leaving elements b[k] through b[b.size()-1] unaffected.
//
// The read(b) method has the same effect as:
//
// read(b, 0, b.length)
// read(b, 0, b.size())
//
// Overrides:
// read in class FilterInputStream
@@ -49,14 +49,14 @@ int DataInputStream::read() {
// 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.
int DataInputStream::read(byteArray b) {
int DataInputStream::read(std::vector<uint8_t>& b) {
if (stream == nullptr) {
fprintf(stderr,
"DataInputStream::read(byteArray) called but underlying stream is "
"DataInputStream::read(std::vector<uint8_t>) called but underlying stream is "
"nullptr\n");
return -1;
}
return read(b, 0, b.length);
return read(b, 0, b.size());
}
// Reads up to len bytes of data from the contained input stream into an array
@@ -77,7 +77,7 @@ int DataInputStream::read(byteArray b) {
// b[off+len-1] unaffected.
//
// In every case, elements b[0] through b[off] and elements b[off+len] through
// b[b.length-1] are unaffected.
// b[b.size()-1] are unaffected.
//
// Overrides:
// read in class FilterInputStream
@@ -88,11 +88,11 @@ int DataInputStream::read(byteArray b) {
// 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.
int DataInputStream::read(byteArray b, unsigned int offset,
int DataInputStream::read(std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
if (stream == nullptr) {
fprintf(stderr,
"DataInputStream::read(byteArray,offset,length) called but "
"DataInputStream::read(std::vector<uint8_t>,offset,length) called but "
"underlying stream is nullptr\n");
return -1;
}
@@ -168,10 +168,10 @@ wchar_t DataInputStream::readChar() {
// b. The number of bytes read is equal to the length of b. This method blocks
// until one of the following conditions occurs:
//
// b.length bytes of input data are available, in which case a normal return is
// b.size() bytes of input data are available, in which case a normal return is
// made. End of file is detected, in which case an EOFException is thrown. An
// I/O error occurs, in which case an IOException other than EOFException is
// thrown. If b is null, a NullPointerException is thrown. If b.length is zero,
// thrown. If b is null, a NullPointerException is thrown. If b.size() is zero,
// then no bytes are read. Otherwise, the first byte read is stored into element
// b[0], the next one into b[1], and so on. If an exception is thrown from this
// method, then it may be that some but not all bytes of b have been updated
@@ -179,18 +179,18 @@ wchar_t DataInputStream::readChar() {
//
// Parameters:
// b - the buffer into which the data is read.
bool DataInputStream::readFully(byteArray b) {
bool DataInputStream::readFully(std::vector<uint8_t>& b) {
// TODO 4J Stu - I am not entirely sure if this matches the implementation
// of the Java library
// TODO 4J Stu - Need to handle exceptions here is we throw them in other
// InputStreams
if (stream == nullptr) {
fprintf(stderr,
"DataInputStream::readFully(byteArray) but underlying stream is "
"DataInputStream::readFully(std::vector<uint8_t>) but underlying stream is "
"nullptr\n");
return false;
}
for (unsigned int i = 0; i < b.length; i++) {
for (unsigned int i = 0; i < b.size(); i++) {
int byteRead = stream->read();
if (byteRead == -1) {
return false;
@@ -201,18 +201,18 @@ bool DataInputStream::readFully(byteArray b) {
return true;
}
bool DataInputStream::readFully(charArray b) {
bool DataInputStream::readFully(std::vector<char>& b) {
// TODO 4J Stu - I am not entirely sure if this matches the implementation
// of the Java library
// TODO 4J Stu - Need to handle exceptions here is we throw them in other
// InputStreams
if (stream == nullptr) {
fprintf(stderr,
"DataInputStream::readFully(charArray) but underlying stream is "
"DataInputStream::readFully(std::vector<char>) but underlying stream is "
"nullptr\n");
return false;
}
for (unsigned int i = 0; i < b.length; i++) {
for (unsigned int i = 0; i < b.size(); i++) {
int byteRead = stream->read();
if (byteRead == -1) {
return false;

View File

@@ -40,20 +40,20 @@ void DataOutputStream::flush() {
stream->flush();
}
// Writes b.length bytes from the specified byte array to this output stream.
// Writes b.size() 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). Parameters: b - the data.
void DataOutputStream::write(byteArray b) { write(b, 0, b.length); }
// effect as the call write(b, 0, b.size()). Parameters: b - the data.
void DataOutputStream::write(const std::vector<uint8_t>& b) { write(b, 0, b.size()); }
// Writes len bytes from the specified byte array starting at offset off to the
// underlying output stream. If no exception is thrown, the counter written is
// incremented by len. Parameters: b - the data. off - the start offset in the
// data. len - the number of bytes to write.
void DataOutputStream::write(byteArray b, unsigned int offset,
void DataOutputStream::write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
if (stream == nullptr) {
fprintf(stderr,
"DataOutputStream::write(byteArray,...) called but underlying "
"DataOutputStream::write(std::vector<uint8_t>,...) called but underlying "
"stream is nullptr\n");
return;
}
@@ -224,7 +224,7 @@ void DataOutputStream::writeUTF(const std::wstring& str) {
// throw new UTFDataFormatException(
// "encoded string too long: " + utflen + " bytes");
byteArray bytearr(utflen + 2);
std::vector<uint8_t> bytearr(utflen + 2);
bytearr[count++] = (uint8_t)((utflen >> 8) & 0xFF);
bytearr[count++] = (uint8_t)((utflen >> 0) & 0xFF);
@@ -251,7 +251,6 @@ void DataOutputStream::writeUTF(const std::wstring& str) {
}
}
write(bytearr, 0, utflen + 2);
delete[] bytearr.data;
}
// 4J Added

View File

@@ -80,18 +80,18 @@ int FileInputStream::read() {
return static_cast<int>(byteRead);
}
// Reads up to b.length bytes of data from this input stream into an array of
// Reads up to b.size() bytes of data from this input stream into an array of
// bytes. This method blocks until some input is available. Parameters: b - the
// buffer into which the data is read. Returns: the total number of bytes read
// into the buffer, or -1 if there is no more data because the end of the file
// has been reached.
int FileInputStream::read(byteArray b) {
int FileInputStream::read(std::vector<uint8_t>& b) {
if (m_fileHandle == nullptr) {
return -1;
}
const size_t numberOfBytesRead =
std::fread(b.data, 1, b.length, m_fileHandle);
std::fread(b.data(), 1, b.size(), m_fileHandle);
if (std::ferror(m_fileHandle) != 0) {
assert(0);
@@ -110,10 +110,10 @@ int FileInputStream::read(byteArray b) {
// b len - the maximum number of bytes read. Returns: the total number of bytes
// read into the buffer, or -1 if there is no more data because the end of the
// file has been reached.
int FileInputStream::read(byteArray b, unsigned int offset,
int FileInputStream::read(std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
// 4J Stu - We don't want to read any more than the array buffer can hold
assert(length <= (b.length - offset));
assert(length <= (b.size() - offset));
if (m_fileHandle == nullptr) {
return -1;

View File

@@ -58,20 +58,20 @@ void FileOutputStream::write(unsigned int b) {
}
}
// Writes b.length bytes from the specified byte array to this file output
// Writes b.size() bytes from the specified byte array to this file output
// stream. Parameters: b - the data.
void FileOutputStream::write(byteArray b) {
void FileOutputStream::write(const std::vector<uint8_t>& b) {
if (m_fileHandle == nullptr) {
return;
}
const size_t numberOfBytesWritten =
std::fwrite(b.data, 1, b.length, m_fileHandle);
std::fwrite(b.data(), 1, b.size(), m_fileHandle);
const int result = std::ferror(m_fileHandle);
if (result != 0) {
// TODO 4J Stu - Some kind of error handling
} else if (numberOfBytesWritten == 0 || numberOfBytesWritten != b.length) {
} else if (numberOfBytesWritten == 0 || numberOfBytesWritten != b.size()) {
// File pointer is past the end of the file
}
}
@@ -79,10 +79,10 @@ void FileOutputStream::write(byteArray b) {
// Writes len bytes from the specified byte array starting at offset off to this
// file output stream. Parameters: b - the data. off - the start offset in the
// data. len - the number of bytes to write.
void FileOutputStream::write(byteArray b, unsigned int offset,
void FileOutputStream::write(const std::vector<uint8_t>& b, unsigned int offset,
unsigned int length) {
// 4J Stu - We don't want to write any more than the array buffer holds
assert(length <= (b.length - offset));
assert(length <= (b.size() - offset));
if (m_fileHandle == nullptr) {
return;

View File

@@ -64,14 +64,14 @@ int IntBuffer::get(unsigned int index) {
// except that it first checks that there is sufficient space in this buffer and
// it is potentially much more efficient. Parameters: src - The array from which
// ints are to be read offset - The offset within the array of the first int to
// be read; must be non-negative and no larger than array.length length - The
// be read; must be non-negative and no larger than array.size() length - The
// number of ints to be read from the given array; must be non-negative and no
// larger than array.length - offset Returns: This buffer
IntBuffer* IntBuffer::put(intArray* inputArray, unsigned int offset,
// larger than array.size() - offset Returns: This buffer
IntBuffer* IntBuffer::put(std::vector<int>* inputArray, unsigned int offset,
unsigned int length) {
assert(offset + length < inputArray->length);
assert(offset + length < inputArray->size());
std::copy(inputArray->data + offset, inputArray->data + offset + length,
std::copy(inputArray->data() + offset, inputArray->data() + offset + length,
buffer + m_position);
m_position += length;
@@ -79,14 +79,14 @@ IntBuffer* IntBuffer::put(intArray* inputArray, unsigned int offset,
return this;
}
IntBuffer* IntBuffer::put(intArray inputArray) {
if (inputArray.length > remaining())
IntBuffer* IntBuffer::put(std::vector<int>& inputArray) {
if (inputArray.size() > remaining())
assert(false); // TODO 4J Stu - Some kind of exception?
std::copy(inputArray.data, inputArray.data + inputArray.length,
std::copy(inputArray.data(), inputArray.data() + inputArray.size(),
buffer + m_position);
m_position += inputArray.length;
m_position += inputArray.size();
return this;
}

View File

@@ -7,37 +7,37 @@
#include "java/System.h"
template <class T>
void System::arraycopy(arrayWithLength<T> src, unsigned int srcPos,
arrayWithLength<T>* dst, unsigned int dstPos,
void System::arraycopy(const std::vector<T>& src, unsigned int srcPos,
std::vector<T>* dst, unsigned int dstPos,
unsigned int length) {
assert(srcPos >= 0 && srcPos <= src.length);
assert(srcPos + length <= src.length);
assert(dstPos + length <= dst->length);
assert(srcPos >= 0 && srcPos <= src.size());
assert(srcPos + length <= src.size());
assert(dstPos + length <= dst->size());
std::copy(src.data + srcPos, src.data + srcPos + length,
dst->data + dstPos);
std::copy(src.data() + srcPos, src.data() + srcPos + length,
dst->data() + dstPos);
}
ArrayCopyFunctionDefinition(Node*) ArrayCopyFunctionDefinition(Biome*)
void System::arraycopy(arrayWithLength<uint8_t> src, unsigned int srcPos,
arrayWithLength<uint8_t>* dst, unsigned int dstPos,
void System::arraycopy(const std::vector<uint8_t>& src, unsigned int srcPos,
std::vector<uint8_t>* dst, unsigned int dstPos,
unsigned int length) {
assert(srcPos >= 0 && srcPos <= src.length);
assert(srcPos + length <= src.length);
assert(dstPos + length <= dst->length);
assert(srcPos >= 0 && srcPos <= src.size());
assert(srcPos + length <= src.size());
assert(dstPos + length <= dst->size());
memcpy(dst->data + dstPos, src.data + srcPos, length);
memcpy(dst->data() + dstPos, src.data() + srcPos, length);
}
void System::arraycopy(arrayWithLength<int> src, unsigned int srcPos,
arrayWithLength<int>* dst, unsigned int dstPos,
void System::arraycopy(const std::vector<int>& src, unsigned int srcPos,
std::vector<int>* dst, unsigned int dstPos,
unsigned int length) {
assert(srcPos >= 0 && srcPos <= src.length);
assert(srcPos + length <= src.length);
assert(dstPos + length <= dst->length);
assert(srcPos >= 0 && srcPos <= src.size());
assert(srcPos + length <= src.size());
assert(dstPos + length <= dst->size());
memcpy(dst->data + dstPos, src.data + srcPos, length * sizeof(int));
memcpy(dst->data() + dstPos, src.data() + srcPos, length * sizeof(int));
}
// TODO 4J Stu - These time functions may suffer from accuracy and we might have