List of usage examples for java.io EOFException EOFException
public EOFException(String s)
EOFException
with the specified detail message. From source file:org.apache.hadoop.hdfs.server.datanode.TestDatanodeProtocolRetryPolicy.java
/** * Verify the following scenario./*from ww w. j a v a 2 s.c om*/ * 1. The initial DatanodeProtocol.registerDatanode succeeds. * 2. DN starts heartbeat process. * 3. In the first heartbeat, NN asks DN to reregister. * 4. DN calls DatanodeProtocol.registerDatanode. * 5. DatanodeProtocol.registerDatanode throws EOFException. * 6. DN retries. * 7. DatanodeProtocol.registerDatanode succeeds. */ @Test(timeout = 60000) public void testDatanodeRegistrationRetry() throws Exception { final DatanodeProtocolClientSideTranslatorPB namenode = mock(DatanodeProtocolClientSideTranslatorPB.class); Mockito.doAnswer(new Answer<DatanodeRegistration>() { int i = 0; @Override public DatanodeRegistration answer(InvocationOnMock invocation) throws Throwable { i++; if (i > 1 && i < 5) { LOG.info("mockito exception " + i); throw new EOFException("TestDatanodeProtocolRetryPolicy"); } else { DatanodeRegistration dr = (DatanodeRegistration) invocation.getArguments()[0]; datanodeRegistration = new DatanodeRegistration(dr.getDatanodeUuid(), dr); LOG.info("mockito succeeded " + datanodeRegistration); return datanodeRegistration; } } }).when(namenode).registerDatanode(Mockito.any(DatanodeRegistration.class)); when(namenode.versionRequest()).thenReturn(new NamespaceInfo(1, CLUSTER_ID, POOL_ID, 1L)); Mockito.doAnswer(new Answer<HeartbeatResponse>() { int i = 0; @Override public HeartbeatResponse answer(InvocationOnMock invocation) throws Throwable { i++; HeartbeatResponse heartbeatResponse; if (i == 1) { LOG.info("mockito heartbeatResponse registration " + i); heartbeatResponse = new HeartbeatResponse(new DatanodeCommand[] { RegisterCommand.REGISTER }, new NNHAStatusHeartbeat(HAServiceState.ACTIVE, 1), null, ThreadLocalRandom.current().nextLong() | 1L); } else { LOG.info("mockito heartbeatResponse " + i); heartbeatResponse = new HeartbeatResponse(new DatanodeCommand[0], new NNHAStatusHeartbeat(HAServiceState.ACTIVE, 1), null, ThreadLocalRandom.current().nextLong() | 1L); } return heartbeatResponse; } }).when(namenode).sendHeartbeat(Mockito.any(DatanodeRegistration.class), Mockito.any(StorageReport[].class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(), Mockito.any(VolumeFailureSummary.class), Mockito.anyBoolean()); dn = new DataNode(conf, locations, null) { @Override DatanodeProtocolClientSideTranslatorPB connectToNN(InetSocketAddress nnAddr) throws IOException { Assert.assertEquals(NN_ADDR, nnAddr); return namenode; } }; // Trigger a heartbeat so that it acknowledges the NN as active. dn.getAllBpOs().get(0).triggerHeartbeatForTests(); waitForBlockReport(namenode); }
From source file:com.google.dart.tools.designer.model.HtmlRenderHelper.java
/** * Read the requested number of characters or fail if there are not enough left. * <p>//from w w w .j a v a 2 s.c om * TODO(scheglov) update to modern Commons-IO which has this method. */ private static void readFully(Reader input, char[] buffer) throws IOException { int length = buffer.length; int remaining = length; while (remaining > 0) { int location = length - remaining; int count = input.read(buffer, location, remaining); if (count == -1) { throw new EOFException("Length to read: " + length + " actual: " + (length - remaining)); } remaining -= count; } }
From source file:org.apache.hadoop.hive.ql.io.TeradataBinaryRecordReader.java
/** * Reads the next key/value pair from the input for processing. * * @param key the key to read data into//from w w w . jav a 2s . c o m * @param value the value to read data into * @return true iff a key/value was read, false if at EOF */ @Override public synchronized boolean next(NullWritable key, BytesWritable value) throws IOException { /* read the record length */ int lengthExpected = recordLengthBytes.length; int hasConsumed = readExpectedBytes(recordLengthBytes, lengthExpected); if (hasConsumed == 0) { LOG.info("Reach the End of File. No more record"); return false; } else if (hasConsumed < lengthExpected) { LOG.error(format("We expect %s bytes for the record length but read %d byte and reach the End of File.", lengthExpected, hasConsumed)); LOG.error(format("The current position in the file : %s", getFilePosition())); LOG.error(format("The current consumed bytes: %s", pos)); LOG.error(format("The bytes for the current record is: %s", Hex.encodeHexString(recordLengthBytes))); throw new EOFException("When reading the record length, reach the unexpected end of file."); } /* get the record contend length to prepare to read the content */ recordLength = EndianUtils.readSwappedUnsignedShort(recordLengthBytes, 0); pos += lengthExpected; /* read the record content */ lengthExpected = recordLength; hasConsumed = readExpectedBytes(valueByteArray, lengthExpected); if (hasConsumed < lengthExpected) { LOG.error( format("We expect %s bytes for the record content but read %d byte and reach the End of File.", lengthExpected, hasConsumed)); LOG.error(format("The current position in the file : %s", getFilePosition())); LOG.error(format("The current consumed bytes: %s", pos)); LOG.error(format("The bytes for the current record is: %s", Hex.encodeHexString(recordLengthBytes) + Hex.encodeHexString(valueByteArray))); throw new EOFException("When reading the contend of the record, reach the unexpected end of file."); } value.set(valueByteArray, 0, recordLength); pos += lengthExpected; /* read the record end */ lengthExpected = endOfRecord.length; hasConsumed = readExpectedBytes(endOfRecord, lengthExpected); if (hasConsumed < lengthExpected) { LOG.error(format( "We expect %s bytes for the record end symbol but read %d byte and reach the End of File.", lengthExpected, hasConsumed)); LOG.error(format("The current position in the file : %s", getFilePosition())); LOG.error(format("The current consumed bytes: %s", pos)); LOG.error(format("The bytes for the current record is: %s", Hex.encodeHexString(recordLengthBytes) + Hex.encodeHexString(valueByteArray) + Hex.encodeHexString(endOfRecord))); throw new EOFException("When reading the end of record, reach the unexpected end of file."); } if (endOfRecord[0] != TeradataBinaryFileOutputFormat.RECORD_END_BYTE) { throw new IOException( format("We expect 0x0a as the record end but get %s.", Hex.encodeHexString(endOfRecord))); } pos += lengthExpected; return true; }
From source file:org.cryptonode.jncryptor.TestVectorReader.java
/** * Returns a trimmed version of the next non-blank line that doesn't begin * with a comment character.// w w w. j a va 2 s .c o m * * @param iterator * @param throwExceptionIfMissing * TODO * @return the next string, or <code>null</code> if there is none */ private static String readNextNonCommentLine(Iterator<String> iterator, boolean throwExceptionIfMissing) throws IOException { while (iterator.hasNext()) { String trimmed = iterator.next().trim(); if (trimmed.isEmpty()) { continue; } if (!trimmed.startsWith(COMMENT_CHAR)) { return trimmed; } } if (throwExceptionIfMissing) { throw new EOFException("Failed to read next non-comment line."); } return null; }
From source file:org.apache.hadoop.hive.serde2.teradata.TeradataBinaryDataInputStream.java
/** * Read DECIMAL(P, S).//from ww w . j a va 2 s. co m * The representation of decimal in Teradata binary format is * the byte number to read is decided solely by the precision(P), * HiveDecimal is constructed through the byte array and scale. * the null DECIMAL will use 0x00 to pad. * * @param scale the scale * @param byteNum the byte num * @return the hive decimal * @throws IOException the io exception */ public HiveDecimal readDecimal(int scale, int byteNum) throws IOException { byte[] decimalContent = new byte[byteNum]; int numOfBytesRead = in.read(decimalContent); if (decimalContent.length != 0 && numOfBytesRead != byteNum) { throw new EOFException( format("Fail to read the decimal. Expect %d bytes, get %d bytes", byteNum, numOfBytesRead)); } ArrayUtils.reverse(decimalContent); return HiveDecimal.create(new BigInteger(decimalContent), scale); }
From source file:net.minecraftforge.fml.repackage.com.nothome.delta.GDiffPatcher.java
private void copy(long offset, int length, SeekableSource source, OutputStream output) throws IOException { source.seek(offset);/* ww w . j a v a 2 s .co m*/ while (length > 0) { int len = Math.min(buf.capacity(), length); buf.clear().limit(len); int res = source.read(buf); if (res == -1) throw new EOFException("in copy " + offset + " " + length); output.write(buf.array(), 0, res); length -= res; } }
From source file:com.microsoft.azure.storage.analytics.LogRecordStreamReader.java
/** * Read a delimiter from the stream.//from w w w .jav a2 s . co m * * @param delimiter * the delimiter to read. * @throws IOException */ private void readDelimiter(char delimiter) throws IOException { if (this.isEndOfFile()) { throw new EOFException(SR.LOG_STREAM_END_ERROR); } else { int read = this.read(); if (read == -1 || (char) read != delimiter) { throw new IllegalStateException(SR.LOG_STREAM_DELIMITER_ERROR); } } }
From source file:net.minecraftforge.fml.repackage.com.nothome.delta.GDiffPatcher.java
private void append(int length, InputStream patch, OutputStream output) throws IOException { while (length > 0) { int len = Math.min(buf2.length, length); int res = patch.read(buf2, 0, len); if (res == -1) throw new EOFException("cannot read " + length); output.write(buf2, 0, res);//from ww w. j a v a 2 s . c om length -= res; } }
From source file:com.examples.with.different.packagename.concolic.MathRuntimeException.java
/** * Constructs a new <code>EOFException</code> with specified formatted detail message. * Message formatting is delegated to {@link java.text.MessageFormat}. * @param pattern format specifier//w w w . j a v a2 s . co m * @param arguments format arguments * @return built exception */ public static EOFException createEOFException(final String pattern, final Object... arguments) { return new EOFException(buildMessage(Locale.US, pattern, arguments)) { /** Serializable version identifier. */ private static final long serialVersionUID = 279461544586092584L; /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return buildMessage(Locale.getDefault(), pattern, arguments); } }; }
From source file:com.microsoft.azure.storage.analytics.LogRecordStreamReader.java
/** * Checks to see if the next character is the delimiter expected. * /*from www .j a v a 2 s . c o m*/ * @param delimiter * the delimiter to try to peek. * @return * @throws IOException */ private boolean tryPeekDelimiter(char delimiter) throws IOException { if (this.isEndOfFile()) { throw new EOFException(SR.LOG_STREAM_END_ERROR); } else { if ((char) this.peek() != delimiter) { return false; } else { return true; } } }