forked from suyashkumar/dicom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
249 lines (220 loc) · 6.72 KB
/
Copy pathreader.go
File metadata and controls
249 lines (220 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package dicomio
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"github.com/suyashkumar/dicom/pkg/charset"
"golang.org/x/text/encoding"
)
var (
// ErrorInsufficientBytesLeft indicates there are not enough bytes left in
// the current buffer (or enough bytes left until the currently set limit)
// to complete the operation.
ErrorInsufficientBytesLeft = errors.New("not enough bytes left until buffer limit to complete this operation")
)
// LimitReadUntilEOF is a special dicomio.Reader limit indicating that there is no hard limit and the
// Reader should read until EOF.
const LimitReadUntilEOF = -9999
// Reader provides common functionality for reading underlying DICOM data.
type Reader interface {
io.Reader
// ReadUInt8 reads a uint16 from the underlying reader.
ReadUInt8() (uint8, error)
// ReadUInt16 reads a uint16 from the underlying reader.
ReadUInt16() (uint16, error)
// ReadUInt32 reads a uint32 from the underlying reader.
ReadUInt32() (uint32, error)
// ReadInt16 reads a int16 from the underlying reader.
ReadInt16() (int16, error)
// ReadInt32 reads a int32 from the underlying reader.
ReadInt32() (int32, error)
// ReadFloat32 reads a float32 from the underlying reader.
ReadFloat32() (float32, error)
// ReadFloat64 reads a float32 from the underlying reader.
ReadFloat64() (float64, error)
// ReadString reads an n byte string from the underlying reader.
// Uses the charset.CodingSystem encoding decoders to read the string, if
// set.
ReadString(n uint32) (string, error)
// Skip skips the reader ahead by n bytes.
Skip(n int64) error
// Peek returns the next n bytes without advancing the reader. This will
// return bufio.ErrBufferFull if the buffer is full.
Peek(n int) ([]byte, error)
// PushLimit sets a read limit of n bytes from the current position of the
// reader. Once the limit is reached, IsLimitExhausted will return true, and
// other attempts to read data from dicomio.Reader will return io.EOF.
PushLimit(n int64) error
// PopLimit removes the most recent limit set, and restores the limit before
// that one.
PopLimit()
// IsLimitExhausted indicates whether or not we have read up to the
// currently set limit position.
IsLimitExhausted() bool
// BytesLeftUntilLimit returns the number of bytes remaining until we reach
// the currently set limit position.
BytesLeftUntilLimit() int64
// SetTransferSyntax sets the byte order and whether the current transfer
// syntax is implicit or not.
SetTransferSyntax(bo binary.ByteOrder, implicit bool)
// IsImplicit returns if the currently set transfer syntax on this Reader is
// implicit or not.
IsImplicit() bool
// SetCodingSystem sets the charset.CodingSystem to be used when ReadString
// is called.
SetCodingSystem(cs charset.CodingSystem)
ByteOrder() binary.ByteOrder
}
type reader struct {
in *bufio.Reader
bo binary.ByteOrder
implicit bool
limit int64
bytesRead int64
limitStack []int64
// cs represents the CodingSystem to use when reading the string. If a
// particular encoding.Decoder within this CodingSystem is nil, assume
// ASCII.
cs charset.CodingSystem
}
// NewReader creates and returns a new dicomio.Reader.
func NewReader(in *bufio.Reader, bo binary.ByteOrder, limit int64) Reader {
return &reader{
in: in,
bo: bo,
limit: limit,
bytesRead: 0,
}
}
func (r *reader) BytesLeftUntilLimit() int64 {
if r.limit == LimitReadUntilEOF {
return math.MaxInt64
}
return r.limit - r.bytesRead
}
func (r *reader) Read(p []byte) (int, error) {
// Check if we've hit the limit
if r.BytesLeftUntilLimit() <= 0 {
if len(p) == 0 {
return 0, nil
}
return 0, io.EOF
}
// If asking for more than we have left, just return whatever we've got left
// TODO: return a special kind of error if this situation occurs to inform the caller
if int64(len(p)) > r.BytesLeftUntilLimit() {
p = p[:r.BytesLeftUntilLimit()]
}
n, err := r.in.Read(p)
if n >= 0 {
r.bytesRead += int64(n)
}
return n, err
}
func (r *reader) ReadUInt8() (uint8, error) {
var out uint8
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadUInt16() (uint16, error) {
var out uint16
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadUInt32() (uint32, error) {
var out uint32
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadInt16() (int16, error) {
var out int16
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadInt32() (int32, error) {
var out int32
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadFloat32() (float32, error) {
var out float32
err := binary.Read(r, r.bo, &out)
return out, err
}
func (r *reader) ReadFloat64() (float64, error) {
var out float64
err := binary.Read(r, r.bo, &out)
return out, err
}
func internalReadString(data []byte, d *encoding.Decoder) (string, error) {
if len(data) == 0 {
return "", nil
}
if d == nil {
// Assume ASCII
return string(data), nil
}
bytes, err := d.Bytes(data)
if err != nil {
return "", err
}
return string(bytes), nil
}
func (r *reader) ReadString(n uint32) (string, error) {
data := make([]byte, n)
_, err := io.ReadFull(r, data)
if err != nil {
return "", err
}
return internalReadString(data, r.cs.Ideographic)
}
func (r *reader) Skip(n int64) error {
if r.BytesLeftUntilLimit() < n {
// not enough left to skip
return ErrorInsufficientBytesLeft
}
_, err := io.CopyN(io.Discard, r, n)
return err
}
// PushLimit creates a limit n bytes from the current position
func (r *reader) PushLimit(n int64) error {
newLimit := r.bytesRead + n
if newLimit > r.limit && r.limit != LimitReadUntilEOF {
return fmt.Errorf("new limit exceeds current limit of buffer, new limit: %d, limit: %d", newLimit, r.limit)
}
// Add current limit to the stack
r.limitStack = append(r.limitStack, r.limit)
r.limit = newLimit
return nil
}
func (r *reader) PopLimit() {
if r.bytesRead < r.limit && r.limit != LimitReadUntilEOF {
// didn't read all the way to the limit, so skip over what's left.
_ = r.Skip(r.limit - r.bytesRead)
}
// TODO: return an error if trying to Pop the last limit off the slice
last := len(r.limitStack) - 1
r.limit = r.limitStack[last]
r.limitStack = r.limitStack[:last]
}
func (r *reader) IsLimitExhausted() bool {
// if limit < 0 than we should read until EOF
return (r.BytesLeftUntilLimit() <= 0 && r.limit != LimitReadUntilEOF)
}
func (r *reader) SetTransferSyntax(bo binary.ByteOrder, implicit bool) {
r.bo = bo
r.implicit = implicit
}
func (r *reader) IsImplicit() bool { return r.implicit }
func (r *reader) SetCodingSystem(cs charset.CodingSystem) {
r.cs = cs
}
func (r *reader) Peek(n int) ([]byte, error) {
return r.in.Peek(n)
}
func (r *reader) ByteOrder() binary.ByteOrder {
return r.bo
}