use of java.io.EOFException in project lucene-solr by apache.
the class TestAllFilesDetectTruncation method truncateOneFile.
private void truncateOneFile(Directory dir, String victim) throws IOException {
try (BaseDirectoryWrapper dirCopy = newDirectory()) {
dirCopy.setCheckIndexOnClose(false);
long victimLength = dir.fileLength(victim);
int lostBytes = TestUtil.nextInt(random(), 1, (int) Math.min(100, victimLength));
assert victimLength > 0;
if (VERBOSE) {
System.out.println("TEST: now truncate file " + victim + " by removing " + lostBytes + " of " + victimLength + " bytes");
}
for (String name : dir.listAll()) {
if (name.equals(victim) == false) {
dirCopy.copyFrom(dir, name, name, IOContext.DEFAULT);
} else {
try (IndexOutput out = dirCopy.createOutput(name, IOContext.DEFAULT);
IndexInput in = dir.openInput(name, IOContext.DEFAULT)) {
out.copyBytes(in, victimLength - lostBytes);
}
}
dirCopy.sync(Collections.singleton(name));
}
try {
// NOTE: we .close so that if the test fails (truncation not detected) we don't also get all these confusing errors about open files:
DirectoryReader.open(dirCopy).close();
fail("truncation not detected after removing " + lostBytes + " bytes out of " + victimLength + " for file " + victim);
} catch (CorruptIndexException | EOFException e) {
// expected
}
// CheckIndex should also fail:
try {
TestUtil.checkIndex(dirCopy, true, true, null);
fail("truncation not detected after removing " + lostBytes + " bytes out of " + victimLength + " for file " + victim);
} catch (CorruptIndexException | EOFException e) {
// expected
}
}
}
use of java.io.EOFException in project lucene-solr by apache.
the class BufferedIndexInput method refill.
private void refill() throws IOException {
long start = bufferStart + bufferPosition;
long end = start + bufferSize;
if (// don't read past EOF
end > length())
end = length();
int newLength = (int) (end - start);
if (newLength <= 0)
throw new EOFException("read past EOF: " + this);
if (buffer == null) {
// allocate buffer lazily
newBuffer(new byte[bufferSize]);
seekInternal(bufferStart);
}
readInternal(buffer, 0, newLength);
bufferLength = newLength;
bufferStart = start;
bufferPosition = 0;
}
use of java.io.EOFException in project lucene-solr by apache.
the class BufferedIndexInput method readBytes.
@Override
public final void readBytes(byte[] b, int offset, int len, boolean useBuffer) throws IOException {
int available = bufferLength - bufferPosition;
if (len <= available) {
// the buffer contains enough data to satisfy this request
if (// to allow b to be null if len is 0...
len > 0)
System.arraycopy(buffer, bufferPosition, b, offset, len);
bufferPosition += len;
} else {
// the buffer does not have enough data. First serve all we've got.
if (available > 0) {
System.arraycopy(buffer, bufferPosition, b, offset, available);
offset += available;
len -= available;
bufferPosition += available;
}
// and now, read the remaining 'len' bytes:
if (useBuffer && len < bufferSize) {
// If the amount left to read is small enough, and
// we are allowed to use our buffer, do it in the usual
// buffered way: fill the buffer and copy from it:
refill();
if (bufferLength < len) {
// Throw an exception when refill() could not read len bytes:
System.arraycopy(buffer, 0, b, offset, bufferLength);
throw new EOFException("read past EOF: " + this);
} else {
System.arraycopy(buffer, 0, b, offset, len);
bufferPosition = len;
}
} else {
// The amount left to read is larger than the buffer
// or we've been asked to not use our buffer -
// there's no performance reason not to read it all
// at once. Note that unlike the previous code of
// this function, there is no need to do a seek
// here, because there's no need to reread what we
// had in the buffer.
long after = bufferStart + bufferPosition + len;
if (after > length())
throw new EOFException("read past EOF: " + this);
readInternal(b, offset, len);
bufferStart = after;
bufferPosition = 0;
// trigger refill() on read
bufferLength = 0;
}
}
}
use of java.io.EOFException in project geode by apache.
the class ServerHandShakeProcessor method readClientVersion.
private static Version readClientVersion(ServerConnection connection) throws IOException, VersionException {
Socket socket = connection.getSocket();
int timeout = connection.getHandShakeTimeout();
int soTimeout = -1;
try {
soTimeout = socket.getSoTimeout();
socket.setSoTimeout(timeout);
InputStream is = socket.getInputStream();
short clientVersionOrdinal = Version.readOrdinalFromInputStream(is);
if (clientVersionOrdinal == -1) {
throw new EOFException(LocalizedStrings.ServerHandShakeProcessor_HANDSHAKEREADER_EOF_REACHED_BEFORE_CLIENT_VERSION_COULD_BE_READ.toLocalizedString());
}
Version clientVersion = null;
try {
clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
} catch (UnsupportedVersionException uve) {
// Allows higher version of wan site to connect to server
if (connection.getCommunicationMode() == Acceptor.GATEWAY_TO_GATEWAY && !(clientVersionOrdinal == Version.NOT_SUPPORTED_ORDINAL)) {
return Acceptor.VERSION;
} else {
SocketAddress sa = socket.getRemoteSocketAddress();
String sInfo = "";
if (sa != null) {
sInfo = " Client: " + sa.toString() + ".";
}
throw new UnsupportedVersionException(uve.getMessage() + sInfo);
}
}
if (!clientVersion.compatibleWith(Acceptor.VERSION)) {
// we can throw this
throw new IncompatibleVersionException(clientVersion, Acceptor.VERSION);
// to restrict
}
// Backward Compatibilty Support to limited no of versions
return clientVersion;
} finally {
if (soTimeout != -1) {
try {
socket.setSoTimeout(soTimeout);
} catch (IOException ignore) {
}
}
}
}
use of java.io.EOFException in project poi by apache.
the class PICT method read.
private byte[] read(byte[] data, int pos) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
Header header = new Header();
header.read(data, pos);
long bs_exp = (long) pos + header.getSize();
long bs_act = bis.skip(bs_exp);
if (bs_exp != bs_act) {
throw new EOFException();
}
byte[] chunk = new byte[4096];
ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize());
InflaterInputStream inflater = new InflaterInputStream(bis);
try {
int count;
while ((count = inflater.read(chunk)) >= 0) {
out.write(chunk, 0, count);
// PICT zip-stream can be erroneous, so we clear the array to determine
// the maximum of read bytes, after the inflater crashed
bytefill(chunk, (byte) 0);
}
} catch (Exception e) {
int lastLen;
for (lastLen = chunk.length - 1; lastLen >= 0 && chunk[lastLen] == 0; lastLen--) ;
if (++lastLen > 0) {
if (header.getWmfSize() > out.size()) {
// sometimes the wmfsize is smaller than the amount of already successfully read bytes
// in this case we take the lastLen as-is, otherwise we truncate it to the given size
lastLen = Math.min(lastLen, header.getWmfSize() - out.size());
}
out.write(chunk, 0, lastLen);
}
// End of picture marker for PICT is 0x00 0xFF
LOG.log(POILogger.ERROR, "PICT zip-stream is invalid, read as much as possible. Uncompressed length of header: " + header.getWmfSize() + " / Read bytes: " + out.size(), e);
} finally {
inflater.close();
}
return out.toByteArray();
}
Aggregations