Skip to content

Commit f7aff76

Browse files
authored
Merge pull request #52 from cwensley/check-eofs
Check for EOF instead of always throwing exceptions when loading files
2 parents 9d94913 + bb9ffa9 commit f7aff76

13 files changed

+29
-14
lines changed

Source/Pablo/Formats/Character/Types/Adf.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override void Load (Stream fs, CharacterDocument doc, CharacterHandler ha
5454
page.Font.Load (br);
5555

5656
WaitEventArgs args = new WaitEventArgs ();
57-
while (true) {
57+
while (!fs.IsEOF()) {
5858
p.X = rClip.Left;
5959
for (int x=0; x<80; x++) {
6060
byte ch = br.ReadByte ();

Source/Pablo/Formats/Character/Types/Ansi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override bool DetectAnimation (Stream stream)
4949
var bs = new BufferedStream (stream, 1024);
5050
var br = new BinaryReader (bs);
5151
int read = 0;
52-
while (read < 2048 && true) {
52+
while (read < 2048 && !stream.IsEOF()) {
5353
byte curByte = br.ReadByte ();
5454
read++;
5555
if (curByte == 27) {

Source/Pablo/Formats/Character/Types/Ansi.load.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void Load(Stream fs, Canvas canvas, Palette palette, WaitEventHandler onW
498498
{
499499
var args = new WaitEventArgs();
500500
byte curByte = br.ReadByte();
501-
while (true && (!fs.CanSeek || fs.Position < fs.Length))
501+
while (!fs.IsEOF())
502502
{
503503
if (onWait != null)
504504
onWait(this, args);

Source/Pablo/Formats/Character/Types/Atascii.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override bool DetectAnimation(Stream stream)
5252
{
5353

5454
var br = new BinaryReader(stream);
55-
while (true)
55+
while (!stream.IsEOF())
5656
{
5757
byte b = br.ReadByte();
5858
switch (b)
@@ -154,7 +154,7 @@ public override void Load(Stream fs, CharacterDocument document, CharacterHandle
154154
var ce = new CanvasElement(32, 7);
155155
p = rClip.Location;
156156
var args = new WaitEventArgs();
157-
while (true)
157+
while (!fs.IsEOF())
158158
{
159159
document.OnWait(args);
160160
if (args.Exit)

Source/Pablo/Formats/Character/Types/Avatar.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override void Load(Stream fs, CharacterDocument doc, CharacterHandler han
6969
try
7070
{
7171
WaitEventArgs args = new WaitEventArgs();
72-
while (true)
72+
while (!fs.IsEOF())
7373
{
7474
doc.OnWait(args);
7575
if (args.Exit)

Source/Pablo/Formats/Character/Types/Binary.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public override void Load(Stream fs, CharacterDocument doc, CharacterHandler han
111111
CanvasElement ce = new CanvasElement(32, 7);
112112
p = rClip.Location;
113113
WaitEventArgs args = new WaitEventArgs();
114-
while (true)
114+
while (!fs.IsEOF())
115115
{
116116
doc.OnWait(args);
117117
if (args.Exit)

Source/Pablo/Formats/Character/Types/CG.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override bool DetectAnimation(Stream stream)
5252
{
5353

5454
var br = new BinaryReader(stream);
55-
while (true)
55+
while (!stream.IsEOF())
5656
{
5757
byte b = br.ReadByte();
5858
switch (b)
@@ -153,7 +153,7 @@ public override void Load(Stream fs, CharacterDocument document, CharacterHandle
153153
var ce = new CanvasElement(32, 7);
154154
p = rClip.Location;
155155
var args = new WaitEventArgs();
156-
while (true)
156+
while (!fs.IsEOF())
157157
{
158158
document.OnWait(args);
159159
if (args.Exit)

Source/Pablo/Formats/Character/Types/CtrlA.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void Load (Stream stream, CharacterDocument doc, Handler handler)
167167
p = rClip.Location;
168168
attr = CanvasElement.Default.Attribute;
169169
try {
170-
while (true) {
170+
while (!stream.IsEOF()) {
171171
var b = br.ReadByte ();
172172

173173
if (b == 1) {

Source/Pablo/Formats/Character/Types/Tundra.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected static Point Load(Stream stream, Canvas canvas, Palette pal)
186186
{
187187
var br = new BinaryReader(stream);
188188
var attr = new Attribute(0, 0);
189-
while (true)
189+
while (!stream.IsEOF())
190190
{
191191
var command = br.ReadByte();
192192

Source/Pablo/Formats/Pix/FormatPix.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override void Load(Stream fs, Document document)
3535

3636
try
3737
{
38-
while (true)
38+
while (!fs.IsEOF())
3939
{
4040
b = br.ReadByte();
4141
switch ((char)b)

Source/Pablo/Formats/Rip/FormatRip.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void Load (Stream stream, RipDocument document, RipHandler handler)
5757

5858
try {
5959
var args = new WaitEventArgs ();
60-
while (true) {
60+
while (!stream.IsEOF()) {
6161
if (document.AnimateView) {
6262
document.OnWait (args);
6363
if (args.Exit)

Source/Pablo/Formats/Rip/LidgrenExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void ReadCommands (this NetIncomingMessage message, RipDocument do
3434
var stream = message.ReadStream ();
3535
var reader = new BinaryReader (stream);
3636
try {
37-
while (true) {
37+
while (!stream.IsEOF()) {
3838
char b = (char)reader.ReadRipByte ();
3939
if (b == '|') {
4040

Source/Pablo/StreamExtensions.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.IO;
2+
3+
namespace Pablo
4+
{
5+
static class StreamExtensions
6+
{
7+
public static bool IsEOF(this Stream stream)
8+
{
9+
if (!stream.CanSeek)
10+
return false;
11+
return stream.Position >= stream.Length;
12+
}
13+
}
14+
}
15+

0 commit comments

Comments
 (0)