Skip to content

Commit d307a35

Browse files
committed
#845 Fix PR suggestions.
1 parent 2a36196 commit d307a35

1 file changed

Lines changed: 42 additions & 9 deletions

File tree

  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/reader/stream

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/reader/stream/FSStream.scala

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,27 @@ class FSStream (fileName: String, fileStartOffset: Long = 0L, fileEndOffset: Lon
3535

3636
override def inputFileName: String = fileName
3737

38+
/**
39+
* Reads and returns the bytes at the beginning of the file that precede the effective stream content.
40+
*
41+
* This method attempts to read the leading bytes (defined by the file start offset) that appear
42+
* before the effective content of the stream. It only performs the read on the first invocation
43+
* and if the file start offset is greater than zero. Subsequent calls will return an empty array
44+
* since the skipped flag is set after the first successful read.
45+
*
46+
* @return an array of bytes containing the skipped start bytes, or an empty array if the bytes
47+
* have already been read, the file start offset is zero or negative, or no bytes could be read.
48+
*/
3849
def getSkippedStartBytes: Array[Byte] = {
39-
if (skipped || fileStartOffset <= 0)
50+
if (fileStartOffset > Int.MaxValue)
51+
throw new IllegalArgumentException(s"fileStartOffset ($fileStartOffset) exceeds maximum supported value (${Int.MaxValue})")
52+
val fileStartOffsetInt = fileStartOffset.toInt
53+
if (skipped || fileStartOffsetInt <= 0)
4054
Array.empty[Byte]
4155
else {
4256
skipped = true
43-
val b = new Array[Byte](fileStartOffset.toInt)
44-
val actual = bytesStream.read(b, 0, fileStartOffset.toInt)
57+
val b = new Array[Byte](fileStartOffsetInt)
58+
val actual = bytesStream.read(b, 0, fileStartOffsetInt)
4559
if (actual <= 0) {
4660
Array.empty[Byte]
4761
} else {
@@ -50,17 +64,36 @@ class FSStream (fileName: String, fileStartOffset: Long = 0L, fileEndOffset: Lon
5064
}
5165
}
5266

67+
/**
68+
* Reads and returns the bytes at the end of the file that follow the effective stream content.
69+
*
70+
* This method attempts to read the trailing bytes (defined by the file end offset) that appear
71+
* after the effective content of the stream. It only performs the read when the byte index has
72+
* reached or exceeded the effective size and the stream has not yet been closed. If the file end
73+
* offset is zero or negative, the stream is closed and an empty array is returned.
74+
*
75+
* @return an array of bytes containing the skipped end bytes, or an empty array if the stream
76+
* has not yet reached the end of the effective content, the stream is already closed,
77+
* the file end offset is zero or negative, or no bytes could be read.
78+
*/
5379
def getSkippedEndBytes: Array[Byte] = {
80+
if (fileEndOffset > Int.MaxValue)
81+
throw new IllegalArgumentException(s"fileEndOffset ($fileEndOffset) exceeds maximum supported value (${Int.MaxValue})")
82+
val fileEndOffsetInt = fileEndOffset.toInt
5483
if (byteIndex >= effectiveSize && !isClosed) {
55-
val b = new Array[Byte](fileEndOffset.toInt)
56-
val actual = bytesStream.read(b, 0, fileEndOffset.toInt)
57-
if (actual <= 0) {
58-
Array.empty[Byte]
84+
if (fileEndOffsetInt > 0) {
85+
val b = new Array[Byte](fileEndOffsetInt)
86+
val actual = bytesStream.read(b, 0, fileEndOffsetInt)
87+
if (actual <= 0) {
88+
Array.empty[Byte]
89+
} else {
90+
b.take(actual)
91+
}
5992
} else {
60-
b.take(actual)
93+
close()
94+
Array.empty[Byte]
6195
}
6296
} else {
63-
close()
6497
Array.empty[Byte]
6598
}
6699
}

0 commit comments

Comments
 (0)