InputStream.java
/*
** Module : InputStream.java
** Abstract : Implementation of the builtin class.
**
** Copyright (c) 2019-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 IAS 20190923 First version.
** 002 MP 20200703 Complete code with methods like legacy class
** 003 ME 20201209 Fully implement non-abstract methods.
** ME 20210204 Add destructor to automatically close the stream.
** 004 CA 20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
** signature.
** 005 CA 20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
** dropped if is a no-op.
*/
/*
** 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.oo.lang.*;
import com.goldencode.p2j.util.*;
import com.goldencode.p2j.util.BlockManager.Action;
import com.goldencode.p2j.util.BlockManager.Condition;
import com.goldencode.p2j.util.InternalEntry.Type;
import static com.goldencode.p2j.report.ReportConstants.*;
import static com.goldencode.p2j.util.BlockManager.externalProcedure;
import static com.goldencode.p2j.util.BlockManager.function;
import static com.goldencode.p2j.util.BlockManager.internalProcedure;
import static com.goldencode.p2j.util.BlockManager.onBlockLevel;
import static com.goldencode.p2j.util.BlockManager.returnNormal;
import static com.goldencode.p2j.util.BlockManager.undoThrow;
/**
* Business logic (converted to Java from the 4GL source code
* in Progress/IO/InputStream.cls).
*/
@LegacyResource(resource = "Progress.IO.InputStream")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public abstract class InputStream
extends BaseObject
{
@LegacySignature(type = Type.PROPERTY, name = "Closed")
private final logical closed = TypeFactory.logical(false);
@LegacySignature(type = Type.EXECUTE)
public void __io_InputStream_execute__()
{
onBlockLevel(Condition.ERROR, Action.THROW);
}
@LegacySignature(type = Type.CONSTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __io_InputStream_constructor__()
{
internalProcedure(this, "__io_InputStream_constructor__", new Block((Body) () ->
{
__lang_BaseObject_constructor__();
}));
}
@LegacySignature(type = Type.DESTRUCTOR)
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void __io_InputStream_destructor__()
{
internalProcedure(this, "__io_InputStream_destructor__", new Block((Body) () ->
{
close();
}));
}
@LegacySignature(returns = "LOGICAL", type = Type.GETTER, name = "Closed")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public logical getClosed()
{
return function(this, "Closed", logical.class, new Block((Body) () ->
{
returnNormal(closed);
}));
}
@LegacySignature(type = Type.METHOD, name = "Close")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public void close()
{
internalProcedure(this, "Close", new Block((Body) () ->
{
this.closed.assign(true);
}));
}
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "Read", parameters =
{
@LegacyParameter(name = "target", type = "MEMPTR", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public int64 read(final memptr _target)
{
memptr target = TypeFactory.initInput(_target);
return function(this, "Read", int64.class, new Block((Body) () ->
{
if (closed.booleanValue())
{
ErrorManager.recordOrThrowError(18196, "Cannot invoke method 'Read' after stream has been closed", false);
}
if (target.isUninitialized())
{
ErrorManager.recordOrThrowError(19066, "Invalid parameter: An initialized MEMPTR must be provided for the Read method or constructor.", false);
}
returnNormal(read(target, new int64(1), target.length()));
}));
}
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "Read", parameters = {
@LegacyParameter(name = "target", type = "HANDLE", mode = "INPUT") })
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
public int64 read(final handle target)
{
return function(this, "Read", int64.class, new Block((Body) () -> {
ErrorManager.recordOrThrowError(18192,
"Cannot invoke Read method in 'Progress.IO.InputStream' because it is not implemented",
false);
}));
}
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "Read", parameters =
{
@LegacyParameter(name = "target", 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)
public abstract int64 read(final memptr target, final int64 offset, final int64 length);
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "Read", parameters =
{
@LegacyParameter(name = "delimiter", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "target", type = "CHARACTER", mode = "OUTPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public abstract int64 read(final character delimiter, final character target);
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "Read", parameters =
{
@LegacyParameter(name = "delimiter", type = "CHARACTER", mode = "INPUT"),
@LegacyParameter(name = "target", type = "LONGCHAR", mode = "OUTPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public abstract int64 read(final character delimiter, final longchar target);
@LegacySignature(returns = "INT64", type = Type.METHOD, name = "SkipBytes", parameters =
{
@LegacyParameter(name = "n", type = "INT64", mode = "INPUT")
})
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
public int64 skipBytes(final int64 _n)
{
int64 n = TypeFactory.initInput(_n);
memptr mPtr = TypeFactory.memptr();
return function(this, "SkipBytes", int64.class, new Block((Body) () ->
{
if (closed.booleanValue())
{
ErrorManager.recordOrThrowError(18196, "Cannot invoke method 'SkipBytes' after stream has been closed", false);
}
// consume the number of bytes from input
mPtr.setLength(n);
// offset is zero for skip, maybe a hint for read to discard that data
n.assign(read(mPtr, new int64(0), n));
returnNormal(n);
}, (Fini) () -> {
mPtr.setLength(0);
}));
}
}