[
https://issues.apache.org/jira/browse/AVRO-2127?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16680053#comment-16680053
]
ASF GitHub Bot commented on AVRO-2127:
--------------------------------------
dkulp closed pull request #323: AVRO-2127: throw more specific exceptions from
DataFileStream#initialize
URL: https://github.com/apache/avro/pull/323
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/InvalidAvroMagicException.java
b/lang/java/avro/src/main/java/org/apache/avro/InvalidAvroMagicException.java
new file mode 100644
index 000000000..6519ad800
--- /dev/null
+++
b/lang/java/avro/src/main/java/org/apache/avro/InvalidAvroMagicException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import java.io.IOException;
+
+public class InvalidAvroMagicException extends IOException {
+ public InvalidAvroMagicException(String message) {
+ super(message);
+ }
+}
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/InvalidNumberEncodingException.java
b/lang/java/avro/src/main/java/org/apache/avro/InvalidNumberEncodingException.java
new file mode 100644
index 000000000..fe5408f2d
--- /dev/null
+++
b/lang/java/avro/src/main/java/org/apache/avro/InvalidNumberEncodingException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import java.io.IOException;
+
+public class InvalidNumberEncodingException extends IOException {
+ public InvalidNumberEncodingException(String message) {
+ super(message);
+ }
+}
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/UnknownAvroCodecException.java
b/lang/java/avro/src/main/java/org/apache/avro/UnknownAvroCodecException.java
new file mode 100644
index 000000000..abeb69c40
--- /dev/null
+++
b/lang/java/avro/src/main/java/org/apache/avro/UnknownAvroCodecException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import java.io.IOException;
+
+public class UnknownAvroCodecException extends IOException {
+ public UnknownAvroCodecException(String message) {
+ super(message);
+ }
+}
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
index ba0daa5ea..0c399a9e5 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader.java
@@ -23,6 +23,7 @@
import java.io.File;
import java.util.Arrays;
+import org.apache.avro.InvalidAvroMagicException;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.DatumReader;
import static org.apache.avro.file.DataFileConstants.SYNC_SIZE;
@@ -47,7 +48,7 @@
DatumReader<D> reader)
throws IOException {
if (in.length() < MAGIC.length)
- throw new IOException("Not an Avro data file");
+ throw new InvalidAvroMagicException("Not an Avro data file");
// read magic header
byte[] magic = new byte[MAGIC.length];
@@ -60,7 +61,7 @@
if (Arrays.equals(DataFileReader12.MAGIC, magic)) // 1.2 format
return new DataFileReader12<>(in, reader);
- throw new IOException("Not an Avro data file");
+ throw new InvalidAvroMagicException("Not an Avro data file");
}
/**
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader12.java
b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader12.java
index e285b087f..0c937bff7 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader12.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileReader12.java
@@ -26,7 +26,9 @@
import java.util.Iterator;
import java.util.Map;
+import org.apache.avro.InvalidAvroMagicException;
import org.apache.avro.Schema;
+import org.apache.avro.UnknownAvroCodecException;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.BinaryDecoder;
@@ -68,7 +70,7 @@ public DataFileReader12(SeekableInput sin, DatumReader<D>
reader)
byte[] magic = new byte[4];
in.read(magic);
if (!Arrays.equals(MAGIC, magic))
- throw new IOException("Not a data file.");
+ throw new InvalidAvroMagicException("Not a data file.");
long length = in.length();
in.seek(length-4);
@@ -91,7 +93,7 @@ public DataFileReader12(SeekableInput sin, DatumReader<D>
reader)
this.count = getMetaLong(COUNT);
String codec = getMetaString(CODEC);
if (codec != null && ! codec.equals(NULL_CODEC)) {
- throw new IOException("Unknown codec: " + codec);
+ throw new UnknownAvroCodecException("Unknown codec: " + codec);
}
this.schema = Schema.parse(getMetaString(SCHEMA));
this.reader = reader;
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileStream.java
b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileStream.java
index a7a356109..562158b0c 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/file/DataFileStream.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/file/DataFileStream.java
@@ -33,6 +33,7 @@
import java.util.NoSuchElementException;
import org.apache.avro.AvroRuntimeException;
+import org.apache.avro.InvalidAvroMagicException;
import org.apache.avro.Schema;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DecoderFactory;
@@ -102,7 +103,7 @@ void initialize(InputStream in) throws IOException {
throw new IOException("Not an Avro data file.", e);
}
if (!Arrays.equals(DataFileConstants.MAGIC, magic))
- throw new IOException("Not an Avro data file.");
+ throw new InvalidAvroMagicException("Not an Avro data file.");
long l = vin.readMapStart(); // read meta data
if (l > 0) {
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
b/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
index 7d996d27b..93c147f44 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/BinaryDecoder.java
@@ -23,6 +23,7 @@
import java.nio.ByteBuffer;
import org.apache.avro.AvroRuntimeException;
+import org.apache.avro.InvalidNumberEncodingException;
import org.apache.avro.util.Utf8;
/** An {@link Decoder} for binary-format data.
@@ -151,7 +152,7 @@ public int readInt() throws IOException {
b = buf[pos + len++] & 0xff;
n ^= (b & 0x7f) << 28;
if (b > 0x7f) {
- throw new IOException("Invalid int encoding");
+ throw new InvalidNumberEncodingException("Invalid int encoding");
}
}
}
@@ -223,7 +224,7 @@ private long innerLongDecode(long l) throws IOException {
b = buf[pos + len++] & 0xff;
l ^= (b & 0x7fL) << 63;
if (b > 0x7f) {
- throw new IOException("Invalid long encoding");
+ throw new InvalidNumberEncodingException("Invalid long
encoding");
}
}
}
diff --git
a/lang/java/avro/src/main/java/org/apache/avro/io/DirectBinaryDecoder.java
b/lang/java/avro/src/main/java/org/apache/avro/io/DirectBinaryDecoder.java
index 4577b472c..07fcdb1bd 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/DirectBinaryDecoder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/DirectBinaryDecoder.java
@@ -22,6 +22,7 @@
import java.io.InputStream;
import java.nio.ByteBuffer;
+import org.apache.avro.InvalidNumberEncodingException;
import org.apache.avro.util.ByteBufferInputStream;
@@ -110,7 +111,7 @@ public int readInt() throws IOException {
}
shift += 7;
} while (shift < 32);
- throw new IOException("Invalid int encoding");
+ throw new InvalidNumberEncodingException("Invalid int encoding");
}
@@ -131,7 +132,7 @@ public long readLong() throws IOException {
}
shift += 7;
} while (shift < 64);
- throw new IOException("Invalid long encoding");
+ throw new InvalidNumberEncodingException("Invalid long encoding");
}
private final byte[] buf = new byte[8];
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Throwing more specific exception if an avro file has currupted magic
> --------------------------------------------------------------------
>
> Key: AVRO-2127
> URL: https://issues.apache.org/jira/browse/AVRO-2127
> Project: Apache Avro
> Issue Type: Improvement
> Components: java
> Affects Versions: 1.8.2
> Reporter: Dmitrii Bundin
> Assignee: Dmitrii Bundin
> Priority: Minor
>
> Curently we have IOException thrown if an avro file has incorrect magic.
> It seems reasonable to throw a subclass of IOException to be able to handle
> incorrect magic (in case length are match) in user code.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)