MemoryOutputStream.java
/*
** Module : MemoryOutputStream.java
** Abstract : Implementation of the Progress.IO.MemoryOutputStream builtin class.
**
** Copyright (c) 2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 ICP 20250307 First version.
** 002 ICP 20250328 Added implementations for the builtin functions.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.oo.io;
import com.goldencode.p2j.util.*;
import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.BlockManager.*;
import static com.goldencode.p2j.util.InternalEntry.*;
/**
* Implementation of the Progress.IO.MemoryOutputStream builtin class.
*/
@LegacyResource(resource = "Progress.IO.MemoryOutputStream")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public class MemoryOutputStream
extends OutputStream
{
@LegacySignature(type = Type.PROPERTY, name = "ByteOrder")
private object<? extends ByteOrder> byteOrder = TypeFactory.object(ByteOrder.class);
@LegacySignature(type = Type.PROPERTY, name = "BytesWritten")
private int64 bytesWritten = TypeFactory.int64();
@LegacySignature(type = Type.PROPERTY, name = "Data")
private memptr data = TypeFactory.memptr();
@LegacySignature(type = Type.PROPERTY, name = "Length")
private int64 length = TypeFactory.int64();
/* Flag for knowing if this MemoryOutputStream was initialized with a specific size or not. */
private boolean initializedWithSize = false;
@LegacySignature(type = Type.CONSTRUCTOR, name = "MemoryOutputStream")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void __io_memoryOutputStream_constructor__()
{
__io_OutputStream_constructor__();
initializedWithSize = false;
// Set default ByteOrder to HostByteOrder
byteOrder.assign(ByteOrder.hostByteOrder);
data.setByteOrder(byteOrder.ref().getValue());
}
@LegacySignature(type = Type.CONSTRUCTOR, name = "MemoryOutputStream", parameters =
{
@LegacyParameter(name = "size", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void __io_memoryOutputStream_constructor__(final int64 _size)
{
int64 size = TypeFactory.initInput(_size);
__io_OutputStream_constructor__();
initializedWithSize = true;
// Set default ByteOrder to HostByteOrder
byteOrder.assign(ByteOrder.hostByteOrder);
data.setByteOrder(byteOrder.ref().getValue());
if (size.longValue() > 0)
{
data.setLength(size.longValue());
length.assign(new int64(size));
}
else if (size.longValue() < 0)
{
ErrorManager.recordOrThrowError(18193, "Invalid value specified for parameter 'value' of " +
"method or constructor 'Progress.IO.MemoryOutputStream'",
false);
}
}
@LegacySignature(type = Type.GETTER, name = "ByteOrder", returns = "OBJECT", qualified = "progress.io.byteorder")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public object<? extends ByteOrder> getByteOrder()
{
return byteOrder;
}
@LegacySignature(type = Type.SETTER, name = "ByteOrder", parameters =
{
@LegacyParameter(name = "var", type = "OBJECT", qualified = "progress.io.byteorder", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
private void setByteOrder(final object<? extends ByteOrder> _var)
{
object<? extends ByteOrder> var = TypeFactory.initInput(_var);
byteOrder.assign(var);
data.setByteOrder(byteOrder.ref().getValue());
}
@LegacySignature(type = Type.GETTER, name = "BytesWritten", returns = "INT64")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public int64 getBytesWritten()
{
return bytesWritten.duplicate();
}
@LegacySignature(type = Type.SETTER, name = "BytesWritten", parameters =
{
@LegacyParameter(name = "var", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
private void setBytesWritten(int64 var)
{
bytesWritten.assign(var);
}
@LegacySignature(type = Type.GETTER, name = "Data", returns = "MEMPTR")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public memptr getData()
{
return data;
}
@LegacySignature(type = Type.SETTER, name = "Data", parameters =
{
@LegacyParameter(name = "var", type = "MEMPTR", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
private void setData(final memptr _var)
{
memptr var = TypeFactory.initInput(_var);
data.assign(var);
}
@LegacySignature(type = Type.GETTER, name = "Length", returns = "INT64")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public int64 getLength()
{
return length.duplicate();
}
@LegacySignature(type = Type.SETTER, name = "Length", parameters =
{
@LegacyParameter(name = "var", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
private void setLength(int64 var)
{
length.assign(var);
}
@LegacySignature(type = Type.METHOD, name = "Clear")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void clear_()
{
if (data != null && !data.isUninitialized() && !initializedWithSize)
{
data.setLength(0);
length.assign(new int64(0));
}
bytesWritten.assign(new int64(0));
}
@LegacySignature(type = Type.METHOD, name = "SetByteOrder", parameters =
{
@LegacyParameter(name = "value", type = "OBJECT", qualified = "progress.io.byteorder", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void setByteOrder_1(final object<? extends ByteOrder> _value)
{
object<? extends ByteOrder> value = TypeFactory.initInput(_value);
setByteOrder(_value);
}
@LegacySignature(type = Type.METHOD, name = "Write", returns = "INT64", parameters =
{
@LegacyParameter(name = "value", type = "LONGCHAR", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
@Override
public int64 write(final longchar _value)
{
return write(_value, logical.FALSE);
}
@LegacySignature(type = Type.METHOD, name = "Write", returns = "INT64", parameters =
{
@LegacyParameter(name = "value", type = "CHARACTER", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
@Override
public int64 write(final character _value)
{
return write(_value, logical.FALSE);
}
@LegacySignature(type = Type.METHOD, name = "Write", returns = "INT64", parameters =
{
@LegacyParameter(name = "value", type = "LONGCHAR", mode = "INPUT"),
@LegacyParameter(name = "includeNull", type = "LOGICAL", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public int64 write(final longchar _value, final logical _includeNull)
{
longchar value = TypeFactory.initInput(_value);
logical includeNull = TypeFactory.initInput(_includeNull);
if (value == null || value.isUnknown())
{
ErrorManager.recordOrThrowError(18193,
"Invalid value specified for parameter 'source' of " +
"method or constructor 'Write'",
false);
return new int64(0);
}
String str = value.getValue();
byte[] bytes = str.getBytes();
int totalBytes = bytes.length + (includeNull.booleanValue() ? 1 : 0);
if (data.isUninitialized())
{
data.setLength(totalBytes);
length.assign(new int64(totalBytes));
}
long offset = bytesWritten.longValue();
long required = offset + totalBytes;
if (required > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(required);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(required));
}
data.writeByteRange(bytes, offset, bytes.length, false);
if (includeNull.booleanValue())
{
data.writeByte((byte) 0, offset + bytes.length);
}
bytesWritten.assign(new int64(offset + totalBytes));
return new int64(totalBytes);
}
@LegacySignature(type = Type.METHOD, name = "Write", returns = "INT64", parameters =
{
@LegacyParameter(name = "value", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "includeNull", type = "LOGICAL", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public int64 write(final character _value, final logical _includeNull)
{
character value = TypeFactory.initInput(_value);
logical includeNull = TypeFactory.initInput(_includeNull);
if (value == null || value.isUnknown())
{
ErrorManager.recordOrThrowError(18193,
"Invalid value specified for parameter 'source' of " +
"method or constructor 'Write'",
false);
return new int64(0);
}
String str = value.getValue();
byte[] bytes = str.getBytes();
int totalBytes = bytes.length + (includeNull.booleanValue() ? 1 : 0);
if (data.isUninitialized())
{
data.setLength(totalBytes);
length.assign(new int64(totalBytes));
}
long offset = bytesWritten.longValue();
long required = offset + totalBytes;
if (required > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(required);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(required));
}
data.writeByteRange(bytes, offset, bytes.length, false);
if (includeNull.booleanValue())
{
data.writeByte((byte) 0, offset + bytes.length);
}
bytesWritten.assign(new int64(offset + totalBytes));
return new int64(totalBytes);
}
@LegacySignature(type = Type.METHOD, name = "Write", returns = "INT64", parameters =
{
@LegacyParameter(name = "value", type = "MEMPTR", mode = "INPUT"),
@LegacyParameter(name = "offset", type = "INT64", mode = "INPUT"),
@LegacyParameter(name = "length", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
@Override
public int64 write(final memptr _value, final int64 _offset, final int64 _length_1)
{
memptr value = TypeFactory.initInput(_value);
int64 offset = TypeFactory.initInput(_offset);
int64 length_1 = TypeFactory.initInput(_length_1);
if (value == null || value.isUninitialized())
{
ErrorManager.recordOrThrowError(18193,
"Invalid value specified for parameter 'source' of " +
"method or constructor 'Write'",
false);
return new int64(0);
}
if (offset.isUnknown() || offset.getValue() <= 0)
{
ErrorManager.recordOrThrowError(18193,
"Invalid value specified for parameter 'offset' of " +
"method or constructor 'Write'",
false);
return new int64(0);
}
if (_length_1.isUnknown() || _length_1.getValue() < 0)
{
ErrorManager.recordOrThrowError(18193,
"Invalid value specified for parameter 'length' of " +
"method or constructor 'Write'",
false);
return new int64(0);
}
long len = length_1.longValue();
// 1-based indexing
long srcOffset = offset.longValue() - 1;
long destOffset = bytesWritten.longValue();
long available = value.length().getValue() - srcOffset;
long safeLength = Math.min(available, len);
if (safeLength < 0)
{
safeLength = 0;
}
byte[] bytes = value.readByteRange(srcOffset, safeLength, false);
long required = destOffset + bytes.length;
if (data.isUninitialized())
{
data.setLength(required);
length.assign(new int64(required));
}
else if (required > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(required);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(required));
}
data.writeByteRange(bytes, destOffset, bytes.length, false);
bytesWritten.assign(new int64(destOffset + bytes.length));
return new int64(bytes.length);
}
@LegacySignature(type = Type.METHOD, name = "WriteByte", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeByte(final int64 _value)
{
int64 value = TypeFactory.initInput(_value);
final int SIZE = 1;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
byte b = (byte) (value.longValue() & 0xFF);
data.writeByte(b, offset);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteDouble", parameters =
{
@LegacyParameter(name = "value", type = "DECIMAL", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeDouble(final decimal _value)
{
decimal value = TypeFactory.initInput(_value);
final int SIZE = 8;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
double doubleVal = value.doubleValue();
long longBits = Double.doubleToLongBits(doubleVal);
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
for (int i = 0; i < SIZE; i++)
{
bytes[i] = (byte) ((longBits >> (i * 8)) & 0xFF);
}
}
else
{
for (int i = 0; i < SIZE; i++)
{
bytes[SIZE - 1 - i] = (byte) ((longBits >> (i * 8)) & 0xFF);
}
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteFloat", parameters =
{
@LegacyParameter(name = "value", type = "DECIMAL", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeFloat(final decimal _value)
{
decimal value = TypeFactory.initInput(_value);
final int SIZE = 4;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
float floatVal = (float) value.doubleValue();
int intBits = Float.floatToIntBits(floatVal);
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
bytes[0] = (byte) (intBits & 0xFF);
bytes[1] = (byte) ((intBits >> 8) & 0xFF);
bytes[2] = (byte) ((intBits >> 16) & 0xFF);
bytes[3] = (byte) ((intBits >> 24) & 0xFF);
}
else
{
bytes[3] = (byte) (intBits & 0xFF);
bytes[2] = (byte) ((intBits >> 8) & 0xFF);
bytes[1] = (byte) ((intBits >> 16) & 0xFF);
bytes[0] = (byte) ((intBits >> 24) & 0xFF);
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteInt64", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeInt64(final int64 _value)
{
int64 value = TypeFactory.initInput(_value);
final int SIZE = 8;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
long longVal = value.longValue();
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
for (int i = 0; i < SIZE; i++)
{
bytes[i] = (byte) ((longVal >> (8 * i)) & 0xFF);
}
}
else
{
for (int i = 0; i < SIZE; i++)
{
bytes[SIZE - 1 - i] = (byte) ((longVal >> (8 * i)) & 0xFF);
}
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteLong", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeLong(final int64 _value)
{
final int SIZE = 4;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
int intVal = _value.intValue();
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
bytes[0] = (byte) (intVal & 0xFF);
bytes[1] = (byte) ((intVal >> 8) & 0xFF);
bytes[2] = (byte) ((intVal >> 16) & 0xFF);
bytes[3] = (byte) ((intVal >> 24) & 0xFF);
}
else
{
bytes[3] = (byte) (intVal & 0xFF);
bytes[2] = (byte) ((intVal >> 8) & 0xFF);
bytes[1] = (byte) ((intVal >> 16) & 0xFF);
bytes[0] = (byte) ((intVal >> 24) & 0xFF);
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteShort", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeShort(final int64 _value)
{
int64 value = TypeFactory.initInput(_value);
final int SIZE = 2;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
int shortVal = value.intValue();
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
bytes[0] = (byte) (shortVal & 0xFF);
bytes[1] = (byte) ((shortVal >> 8) & 0xFF);
}
else
{
bytes[1] = (byte) (shortVal & 0xFF);
bytes[0] = (byte) ((shortVal >> 8) & 0xFF);
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteUnsignedLong", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeUnsignedLong(final int64 _value)
{
int64 value = TypeFactory.initInput(_value);
final int SIZE = 4;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
long longVal = value.longValue();
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
bytes[0] = (byte) (longVal & 0xFF);
bytes[1] = (byte) ((longVal >> 8) & 0xFF);
bytes[2] = (byte) ((longVal >> 16) & 0xFF);
bytes[3] = (byte) ((longVal >> 24) & 0xFF);
}
else
{
bytes[3] = (byte) (longVal & 0xFF);
bytes[2] = (byte) ((longVal >> 8) & 0xFF);
bytes[1] = (byte) ((longVal >> 16) & 0xFF);
bytes[0] = (byte) ((longVal >> 24) & 0xFF);
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
@LegacySignature(type = Type.METHOD, name = "WriteUnsignedShort", parameters =
{
@LegacyParameter(name = "value", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public void writeUnsignedShort(final int64 _value)
{
int64 value = TypeFactory.initInput(_value);
final int SIZE = 2;
if (data.isUninitialized())
{
data.setLength(SIZE);
length.assign(new int64(SIZE));
}
long offset = bytesWritten.longValue();
long requiredSize = offset + SIZE;
if (requiredSize > length.longValue())
{
// Save old content
byte[] oldBytes = data.readByteRange(0, bytesWritten.longValue(), false);
// Fully reallocate memptr and copy back old content
data = new memptr();
data.setLength(requiredSize);
data.writeByteRange(oldBytes, 0, oldBytes.length, false);
length.assign(new int64(requiredSize));
}
int shortVal = value.intValue();
byte[] bytes = new byte[SIZE];
boolean littleEndian = byteOrder != null && byteOrder.equals(ByteOrder.littleEndian);
if (littleEndian)
{
bytes[0] = (byte) (shortVal & 0xFF);
bytes[1] = (byte) ((shortVal >> 8) & 0xFF);
}
else
{
bytes[1] = (byte) (shortVal & 0xFF);
bytes[0] = (byte) ((shortVal >> 8) & 0xFF);
}
data.writeByteRange(bytes, offset, SIZE, false);
bytesWritten.assign(new int64(offset + SIZE));
}
}