List of usage examples for java.io EOFException EOFException
public EOFException(String s)
EOFException
with the specified detail message. From source file:com.proofpoint.jaxrs.AbstractMapperTest.java
@Test public void testEOFExceptionReturnsJsonMapperParsingException() throws IOException { try {/*from w ww .ja va 2 s. com*/ mapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() { @Override public int read() throws IOException { throw new EOFException("forced EOF Exception"); } @Override public int read(byte[] b) throws IOException { throw new EOFException("forced EOF Exception"); } @Override public int read(byte[] b, int off, int len) throws IOException { throw new EOFException("forced EOF Exception"); } }); fail("Should have thrown a JsonMapperParsingException"); } catch (JsonMapperParsingException e) { Assert.assertTrue((e.getMessage()).startsWith("Invalid json for Java type")); } }
From source file:io.horizondb.io.files.TruncatedSeekableFileDataInput.java
/** * {@inheritDoc}//from w ww .ja va 2 s . com */ @Override public void seek(long position) throws IOException { if (position >= this.length) { throw new EOFException("seeking position: " + position + " length of the input: " + this.length); } this.input.seek(position + this.offset); }
From source file:io.airlift.jaxrs.TestJsonMapper.java
@Test public void testEOFExceptionReturnsJsonMapperParsingException() throws IOException { try {//ww w . j av a 2 s .c om JsonMapper jsonMapper = new JsonMapper(new ObjectMapper()); jsonMapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() { @Override public int read() throws IOException { throw new EOFException("forced EOF Exception"); } @Override public int read(byte[] b) throws IOException { throw new EOFException("forced EOF Exception"); } @Override public int read(byte[] b, int off, int len) throws IOException { throw new EOFException("forced EOF Exception"); } }); Assert.fail("Should have thrown a JsonMapperParsingException"); } catch (JsonMapperParsingException e) { Assert.assertTrue((e.getMessage()).startsWith("Invalid json for Java type")); } }
From source file:com.espertech.esperio.csv.CSVReader.java
/** * Get the next record from the CSV file. * @return a string array containing the values of the record * @throws EOFException in case no more records can be read (end-of-file has been reached and isLooping is false) * @throws EPException in case of error in reading the CSV file */// w w w. jav a 2s . c o m public String[] getNextRecord() throws EOFException, EPException { try { String[] result = getNextValidRecord(); if (atEOF && result == null) { throw new EOFException("In reading CSV file, reached end-of-file and not looping to the beginning"); } if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) { log.debug(".getNextRecord record==" + Arrays.asList(result)); } return result; } catch (EOFException e) { throw e; } catch (IOException e) { throw new EPException(e); } }
From source file:voldemort.server.niosocket.AsyncRequestHandler.java
@Override protected void read(SelectionKey selectionKey) throws IOException { int count = 0; long startNs = -1; if (logger.isDebugEnabled()) startNs = System.nanoTime(); if ((count = socketChannel.read(inputStream.getBuffer())) == -1) throw new EOFException("EOF for " + socketChannel.socket()); if (logger.isTraceEnabled()) traceInputBufferState("Read " + count + " bytes"); if (count == 0) return;// w ww. ja v a 2 s . c o m // Take note of the position after we read the bytes. We'll need it in // case of incomplete reads later on down the method. final int position = inputStream.getBuffer().position(); // Flip the buffer, set our limit to the current position and then set // the position to 0 in preparation for reading in the RequestHandler. inputStream.getBuffer().flip(); // We have to do this on the first request as we don't know the protocol // yet. if (requestHandler == null) { if (!initRequestHandler(selectionKey)) { return; } } if (streamRequestHandler != null) { // We're continuing an existing streaming request from our last pass // through. So handle it and return. handleStreamRequest(selectionKey); return; } if (!requestHandler.isCompleteRequest(inputStream.getBuffer())) { // Ouch - we're missing some data for a full request, so handle that // and return. handleIncompleteRequest(position); return; } // At this point we have the full request (and it's not streaming), so // rewind the buffer for reading and execute the request. inputStream.getBuffer().rewind(); if (logger.isTraceEnabled()) logger.trace("Starting execution for " + socketChannel.socket()); DataInputStream dataInputStream = new DataInputStream(inputStream); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); streamRequestHandler = requestHandler.handleRequest(dataInputStream, dataOutputStream); if (logger.isDebugEnabled()) { logger.debug("AsyncRequestHandler:read finished request from " + socketChannel.socket().getRemoteSocketAddress() + " handlerRef: " + System.identityHashCode(dataInputStream) + " at time: " + System.currentTimeMillis() + " elapsed time: " + (System.nanoTime() - startNs) + " ns"); } if (streamRequestHandler != null) { // In the case of a StreamRequestHandler, we handle that separately // (attempting to process multiple "segments"). handleStreamRequest(selectionKey); return; } // At this point we've completed a full stand-alone request. So clear // our input buffer and prepare for outputting back to the client. if (logger.isTraceEnabled()) logger.trace("Finished execution for " + socketChannel.socket()); prepForWrite(selectionKey); }
From source file:ch.ivyteam.ivy.maven.util.ClasspathJar.java
private static InputStream getInputStream(ZipInputStream zin, String entry) throws IOException { for (ZipEntry e; (e = zin.getNextEntry()) != null;) { if (e.getName().equals(entry)) { return zin; }/*from w ww .j a v a 2s. com*/ } throw new EOFException("Cannot find " + entry); }
From source file:org.apache.hadoop.hdfs.server.datanode.BlockMetadataHeader.java
/** * Read the header without changing the position of the FileChannel. * * @param fc The FileChannel to read./*ww w .j ava 2 s .co m*/ * @return the Metadata Header. * @throws IOException on error. */ public static BlockMetadataHeader preadHeader(FileChannel fc) throws IOException { final byte arr[] = new byte[getHeaderSize()]; ByteBuffer buf = ByteBuffer.wrap(arr); while (buf.hasRemaining()) { if (fc.read(buf, 0) <= 0) { throw new EOFException("unexpected EOF while reading " + "metadata file header"); } } short version = (short) ((arr[0] << 8) | (arr[1] & 0xff)); DataChecksum dataChecksum = DataChecksum.newDataChecksum(arr, 2); return new BlockMetadataHeader(version, dataChecksum); }
From source file:nl.nn.adapterframework.batch.DelphiStringRecordReader.java
private String readString() throws IOException { int len;//w w w . ja v a2 s . co m len = in.read(); // first read the byte that holds the length of the string if (len < 0) { return null; } if (trace && log.isDebugEnabled()) log.debug("read byte for string length [" + len + "]"); byte[] buf = new byte[len]; // allocate space for the bytes of the string int bytesToRead = len; int pos = 0; while (bytesToRead > 0) { int bytesRead = in.read(buf, pos, bytesToRead); if (bytesRead > 0) { pos += bytesRead; bytesToRead -= bytesRead; } else { String currentResult = null; try { currentResult = new String(buf, charsetName); } catch (Exception e) { currentResult = e.getClass().getName() + ": " + e.getMessage(); } throw new EOFException("unexpected EOF after reading [" + pos + "] bytes of a string of length [" + len + "], current result [" + currentResult + "]"); } } if (pos < stringLength) { if (trace && log.isDebugEnabled()) log.debug("skipping [" + (stringLength - pos) + "] bytes"); in.skip(stringLength - pos); } String result = new String(buf, charsetName); if (StringUtils.isNotEmpty(separatorReplacement)) { result = Misc.replace(result, separator, separatorReplacement); } if (trace && log.isDebugEnabled()) log.debug("read string [" + result + "]"); return result; }
From source file:com.byteatebit.nbserver.task.ReadDelimitedMessageTask.java
protected void read(SelectionKey selectionKey, Consumer<List<String>> callback, Consumer<Exception> exceptionHandler) { try {// ww w . ja v a 2s . c o m if (!selectionKey.isValid() || !selectionKey.isReadable()) return; byteBuffer.clear(); int bytesRead = ((ReadableByteChannel) selectionKey.channel()).read(byteBuffer); if (bytesRead < 0) { LOG.warn( "End of stream reached. Deregistering interest in reads on the selection key invoking exception handler."); invokeExceptionHandler(selectionKey, exceptionHandler, new EOFException("End of stream")); return; } byteBuffer.flip(); if (byteBuffer.remaining() + messageBuffer.size() > maxMessageSize) { LOG.error("Max message size of " + maxMessageSize + " bytes exceeded. Deregistering interest in reads on the selection key invoking exception handler."); invokeExceptionHandler(selectionKey, exceptionHandler, new IllegalStateException("Max message size of " + maxMessageSize + " bytes exceeded")); return; } while (byteBuffer.hasRemaining()) messageBuffer.write(byteBuffer.get()); String messagesString = messageBuffer.toString(charset); messageBuffer.reset(); List<String> messages = new ArrayList<>(); for (String message : splitter.split(messagesString)) messages.add(message); messageBuffer.write(messages.remove(messages.size() - 1).getBytes(charset)); if (!messages.isEmpty()) { selectionKey.interestOps(selectionKey.interestOps() & ~SelectionKey.OP_READ); callback.accept(messages); } } catch (Exception e) { LOG.error("ReadDelimitedMessage task failed", e); invokeExceptionHandler(selectionKey, exceptionHandler, e); } }
From source file:org.apache.hadoop.hdfs.TestShortCircuitLocalRead.java
static void checkFileContent(FileSystem fs, Path name, byte[] expected, int readOffset) throws IOException { FSDataInputStream stm = fs.open(name); byte[] actual = new byte[expected.length - readOffset]; stm.readFully(readOffset, actual);/*from ww w . j av a2 s.c o m*/ checkData(actual, readOffset, expected, "Read 2"); stm.close(); // Now read using a different API. actual = new byte[expected.length - readOffset]; stm = fs.open(name); long skipped = stm.skip(readOffset); Assert.assertEquals(skipped, readOffset); //Read a small number of bytes first. int nread = stm.read(actual, 0, 3); nread += stm.read(actual, nread, 2); //Read across chunk boundary nread += stm.read(actual, nread, 517); checkData(actual, readOffset, expected, nread, "A few bytes"); //Now read rest of it while (nread < actual.length) { int nbytes = stm.read(actual, nread, actual.length - nread); if (nbytes < 0) { throw new EOFException("End of file reached before reading fully."); } nread += nbytes; } checkData(actual, readOffset, expected, "Read 3"); stm.close(); }