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

View File

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

View File

@ -44,7 +44,7 @@ public class BlockOutputStream {
else if (s.equals("deflate")) else if (s.equals("deflate"))
compression = CompressFlags.DEFLATE; compression = CompressFlags.DEFLATE;
else 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 */ /** Write a block with the stream's default compression flag */

View File

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

View File

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

View File

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