Replace Error with adequate Exceptions

Sometimes wrapped in UncheckedIOException to not change the method signatures.
This commit is contained in:
Simon Legner 2021-01-03 23:50:43 +01:00
parent 9985bf46ad
commit d5bdd020c1
6 changed files with 21 additions and 16 deletions

View File

@ -18,6 +18,7 @@
package crosby.binary;
import java.io.UncheckedIOException;
import java.util.Date;
import java.util.List;
@ -26,6 +27,7 @@ import com.google.protobuf.InvalidProtocolBufferException;
import crosby.binary.file.BlockReaderAdapter;
import crosby.binary.file.FileBlock;
import crosby.binary.file.FileBlockPosition;
import crosby.binary.file.FileFormatException;
public abstract class BinaryParser implements BlockReaderAdapter {
protected int granularity;
@ -66,9 +68,7 @@ public abstract class BinaryParser implements BlockReaderAdapter {
parse(primblock);
}
} catch (InvalidProtocolBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Error("ParseError"); // TODO
throw new UncheckedIOException(new FileFormatException(e));
}
}

View File

@ -20,6 +20,7 @@ package crosby.binary;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
@ -143,9 +144,7 @@ public class BinarySerializer implements Closeable, Flushable {
output.write(FileBlock.newInstance("OSMData", message
.toByteString(), null));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Error(e);
throw new UncheckedIOException(e);
} finally {
batch_size = 0;
groups.clear();

View File

@ -44,7 +44,7 @@ public class BlockOutputStream {
else if (s.equals("deflate"))
compression = CompressFlags.DEFLATE;
else
throw new Error("Unknown compression type: " + s);
throw new IllegalArgumentException("Unknown compression type: " + s);
}
/** Write a block with the stream's default compression flag */

View File

@ -18,10 +18,12 @@
package crosby.binary.file;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.zip.Deflater;
@ -48,13 +50,13 @@ public class FileBlock extends FileBlockBase {
if (blob != null && blob.size() > MAX_BODY_SIZE/2) {
System.err.println("Warning: Fileblock has body size too large and may be considered corrupt");
if (blob != null && blob.size() > MAX_BODY_SIZE-1024*1024) {
throw new Error("This file has too many entities in a block. Parsers will reject it.");
throw new IllegalArgumentException("This file has too many entities in a block. Parsers will reject it.");
}
}
if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE/2) {
System.err.println("Warning: Fileblock has indexdata too large and may be considered corrupt");
if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE-512) {
throw new Error("This file header is too large. Parsers will reject it.");
throw new IllegalArgumentException("This file header is too large. Parsers will reject it.");
}
}
return new FileBlock(type, blob, indexdata);
@ -77,7 +79,7 @@ public class FileBlock extends FileBlockBase {
deflater.deflate(out, deflater.getTotalOut(), out.length
- deflater.getTotalOut());
if (!deflater.finished()) {
throw new Error("Internal error in compressor");
throw new UncheckedIOException(new EOFException());
}
}
ByteString compressed = ByteString.copyFrom(out, 0, deflater
@ -103,7 +105,7 @@ public class FileBlock extends FileBlockBase {
if (flags == CompressFlags.DEFLATE)
deflateInto(blobbuilder);
else
throw new Error("Compression flag not understood");
throw new IllegalArgumentException("Compression flag not understood");
}
Fileformat.Blob blob = blobbuilder.build();

View File

@ -21,6 +21,7 @@ import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
@ -56,8 +57,7 @@ public class FileBlockPosition extends FileBlockBase {
try {
decompresser.inflate(buf2);
} catch (DataFormatException e) {
e.printStackTrace();
throw new Error(e);
throw new UncheckedIOException(new FileFormatException(e));
}
assert (decompresser.finished());
decompresser.end();
@ -89,7 +89,7 @@ public class FileBlockPosition extends FileBlockBase {
(new DataInputStream(input)).readFully(buf);
return parseData(buf);
} else {
throw new Error("Random access binary reads require seekability");
throw new IllegalArgumentException("Random access binary reads require seekability");
}
}
@ -98,12 +98,12 @@ public class FileBlockPosition extends FileBlockBase {
* stored.
*/
public ByteString serialize() {
throw new Error("TODO");
throw new UnsupportedOperationException("TODO");
}
/** TODO: Parse a serialized representation of this block reference */
static FileBlockPosition parseFrom(ByteString b) {
throw new Error("TODO");
throw new UnsupportedOperationException("TODO");
}
protected int datasize;

View File

@ -25,6 +25,10 @@ public class FileFormatException extends IOException {
super(string);
}
public FileFormatException(Throwable cause) {
super(cause);
}
/**
*
*/