List of usage examples for java.io EOFException EOFException
public EOFException(String s)
EOFException
with the specified detail message. From source file:org.skife.muckery.mappy.ByteUtils.java
/** * Read exactly buffer.length bytes from the stream into the buffer * * @param stream The stream to read from * @param buffer The buffer to read into *//*w ww. j a v a2 s . co m*/ public static void read(InputStream stream, byte[] buffer) throws IOException { int read = 0; while (read < buffer.length) { int newlyRead = stream.read(buffer, read, buffer.length - read); if (newlyRead == -1) throw new EOFException("Attempt to read " + buffer.length + " bytes failed due to EOF."); read += newlyRead; } }
From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java
/** * HandShake Constructor used by server side connection *///from www.j a va2 s . c o m public HandShake(Socket sock, int timeout, DistributedSystem sys, Version clientVersion, CommunicationMode communicationMode, SecurityService securityService) throws IOException, AuthenticationRequiredException { this.clientVersion = clientVersion; this.system = sys; this.securityService = securityService; { int soTimeout = -1; try { soTimeout = sock.getSoTimeout(); sock.setSoTimeout(timeout); InputStream is = sock.getInputStream(); int valRead = is.read(); // this.code = (byte)is.read(); if (valRead == -1) { throw new EOFException( LocalizedStrings.HandShake_HANDSHAKE_EOF_REACHED_BEFORE_CLIENT_CODE_COULD_BE_READ .toLocalizedString()); } this.code = (byte) valRead; if (this.code != REPLY_OK) { throw new IOException( LocalizedStrings.HandShake_HANDSHAKE_REPLY_CODE_IS_NOT_OK.toLocalizedString()); } try { DataInputStream dis = new DataInputStream(is); DataOutputStream dos = new DataOutputStream(sock.getOutputStream()); this.clientReadTimeout = dis.readInt(); if (clientVersion.compareTo(Version.CURRENT) < 0) { // versioned streams allow object serialization code to deal with older clients dis = new VersionedDataInputStream(dis, clientVersion); dos = new VersionedDataOutputStream(dos, clientVersion); } this.id = ClientProxyMembershipID.readCanonicalized(dis); // Note: credentials should always be the last piece in handshake for // Diffie-Hellman key exchange to work if (clientVersion.compareTo(Version.GFE_603) >= 0) { setOverrides(new byte[] { dis.readByte() }); } else { setClientConflation(dis.readByte()); } // Hitesh if (this.clientVersion.compareTo(Version.GFE_65) < 0 || communicationMode.isWAN()) { this.credentials = readCredentials(dis, dos, sys, this.securityService); } else { this.credentials = this.readCredential(dis, dos, sys); } } catch (IOException ioe) { this.code = -2; throw ioe; } catch (ClassNotFoundException cnfe) { this.code = -3; throw new IOException( LocalizedStrings.HandShake_CLIENTPROXYMEMBERSHIPID_CLASS_COULD_NOT_BE_FOUND_WHILE_DESERIALIZING_THE_OBJECT .toLocalizedString()); } } finally { if (soTimeout != -1) { try { sock.setSoTimeout(soTimeout); } catch (IOException ignore) { } } } } }
From source file:com.aliyun.fs.oss.nat.BufferReader.java
public synchronized void seek(long newpos) throws IOException { if (newpos < 0) { throw new EOFException("negative seek position: " + newpos); }// w w w . ja va 2 s . c om if (pos != newpos) { // the seek is attempting to move to the current position updateInnerStream(newpos); } }
From source file:com.buaa.cfs.fs.FSInputChecker.java
/** * Seek to the given position in the stream. The next read() will be from that position. * <p>/*from www .j a va 2 s . co m*/ * <p>This method may seek past the end of the file. This produces no exception and an attempt to read from the * stream will result in -1 indicating the end of the file. * * @param pos the postion to seek to. * * @throws IOException if an I/O error occurs. ChecksumException if the chunk to seek to is corrupted */ @Override public synchronized void seek(long pos) throws IOException { if (pos < 0) { throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK); } // optimize: check if the pos is in the buffer long start = chunkPos - this.count; if (pos >= start && pos < chunkPos) { this.pos = (int) (pos - start); return; } // reset the current state resetState(); // seek to a checksum boundary chunkPos = getChunkPosition(pos); // scan to the desired position int delta = (int) (pos - chunkPos); if (delta > 0) { readFully(this, new byte[delta], 0, delta); } }
From source file:gov.nasa.ensemble.core.jscience.csvxml.ProfileLoader.java
private void skipCDATA(BufferedReader reader) throws IOException, ProfileLoadingException { // Skips over the line that says "<!CDATA[" and the blank line before it. String line = ""; while (line.isEmpty()) { line = reader.readLine();//from w w w.j av a 2s. co m if (line == null) throw new EOFException("End of file where " + BEGINNING_OF_CDATA_SECTION + " was expected."); } if (!line.contains(BEGINNING_OF_CDATA_SECTION)) { throw new ProfileLoadingException("Expected " + BEGINNING_OF_CDATA_SECTION); } }
From source file:com.funambol.foundation.util.FileSystemDAOHelper.java
/** * Copy from an InputStream to an OutputStream. * * @param input The InputStream/* w w w . j a v a2 s . com*/ * @param output The OutputStream * @param expectedSize The expected number of bytes to copy. If -1 the size * is not checked. * @return the number of bytes copied * @throws IOException if an error occurs */ public long copy(InputStream input, OutputStream output, long expectedSize) throws IOException { byte[] buffer = new byte[COPY_BUFFER_SIZE]; long count = 0; while (true) { int read = input.read(buffer); if (read < 0) { break; } count += read; if (expectedSize >= 0 && count > expectedSize) { throw new EOFException("More bytes (" + count + ") than expected (" + expectedSize + ")"); } output.write(buffer, 0, read); } if (expectedSize >= 0 && count < expectedSize) { throw new EOFException("Less bytes (" + count + ") than expected (" + expectedSize + ")"); } return count; }
From source file:dk.statsbiblioteket.util.LineReader.java
@Override public byte readByte() throws IOException { //log.trace("readByte entered"); checkInputFile();/*from w ww. j a v a 2 s . c o m*/ checkBuffer(); if (eof()) { throw new EOFException("Attempted to read past EOF"); } byte b = buffer.get(); position++; if (position >= bufferStart + bufferSize) { invalidateBuffer(); } return b; }
From source file:com.funambol.foundation.util.FileSystemDAOHelper.java
/** * Writes the InputStream in append to the file starting from the given * position. If the file size does not match the expected value an * EOFException is thrown.//from www . ja v a 2s .co m * * @param f the file to open in writing * @param in the InputStream to copy in the file * @param pos the file-pointer offset * @param expectedSize the expected file size * @throws IOException if an error occurs */ public void copyInRandomAccessFile(File f, InputStream in, long pos, long expectedSize) throws IOException { // // open file for reading and writing: also its metadata will be updated // RandomAccessFile raf = new RandomAccessFile(f, "rws"); raf.seek(pos); byte[] buffer = new byte[1024]; long count = 0; while (true) { int read = in.read(buffer); if (read < 0) { break; } count += read; if (expectedSize >= 0 && count > expectedSize) { throw new EOFException("More bytes (" + count + ") than expected (" + expectedSize + ")"); } raf.write(buffer, 0, read); } raf.close(); if (expectedSize >= 0 && count < expectedSize) { throw new EOFException("Less bytes (" + count + ") than expected (" + expectedSize + ")"); } }
From source file:com.healthmarketscience.jackcess.ImportUtil.java
/** * Splits the given line using the given delimiter pattern and quote * character. May read additional lines for quotes spanning newlines. *//*from w w w .j a va2 s. c om*/ private static Object[] splitLine(String line, Pattern delim, char quote, BufferedReader in, int numColumns) throws IOException { List<String> tokens = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); Matcher m = delim.matcher(line); int idx = 0; while (idx < line.length()) { if (line.charAt(idx) == quote) { // find quoted value sb.setLength(0); ++idx; while (true) { int endIdx = line.indexOf(quote, idx); if (endIdx >= 0) { sb.append(line, idx, endIdx); ++endIdx; if ((endIdx < line.length()) && (line.charAt(endIdx) == quote)) { // embedded quote sb.append(quote); // keep searching idx = endIdx + 1; } else { // done idx = endIdx; break; } } else { // line wrap sb.append(line, idx, line.length()); sb.append(LINE_SEPARATOR); idx = 0; line = in.readLine(); if (line == null) { throw new EOFException("Missing end of quoted value " + sb); } } } tokens.add(sb.toString()); // skip next delim idx = (m.find(idx) ? m.end() : line.length()); } else if (m.find(idx)) { // next unquoted value tokens.add(line.substring(idx, m.start())); idx = m.end(); } else { // trailing token tokens.add(line.substring(idx)); idx = line.length(); } } return tokens.toArray(new Object[Math.max(tokens.size(), numColumns)]); }
From source file:org.apache.hadoop.hive.serde2.teradata.TeradataBinarySerde.java
/** * Deserialize an object out of a Writable blob. In most cases, the return * value of this function will be constant since the function will reuse the * returned object. If the client wants to keep a copy of the object, the * client needs to clone the returned value by calling * ObjectInspectorUtils.getStandardObject(). * * @param blob//from w w w. j a v a 2 s .c o m * The Writable object containing a serialized object * @return A Java object representing the contents in the blob. */ @Override public Object deserialize(Writable blob) throws SerDeException { try { BytesWritable data = (BytesWritable) blob; // initialize the data to be the input stream TeradataBinaryDataInputStream in = new TeradataBinaryDataInputStream( new ByteArrayInputStream(data.getBytes(), 0, data.getLength())); int numOfByteRead = in.read(inForNull); if (inForNull.length != 0 && numOfByteRead != inForNull.length) { throw new EOFException("not enough bytes for one object"); } boolean isNull; for (int i = 0; i < numCols; i++) { // get if the ith field is null or not isNull = ((inForNull[i / 8] & (128 >> (i % 8))) != 0); row.set(i, deserializeField(in, columnTypes.get(i), row.get(i), isNull)); } //After deserializing all the fields, the input should be over in which case in.read will return -1 if (in.read() != -1) { throw new EOFException( "The inputstream has more after we deserialize all the fields - this is unexpected"); } } catch (EOFException e) { LOG.warn("Catch thrown exception", e); LOG.warn("This record has been polluted. We have reset all the row fields to be null"); for (int i = 0; i < numCols; i++) { row.set(i, null); } } catch (IOException e) { throw new SerDeException(e); } catch (ParseException e) { throw new SerDeException(e); } return row; }