Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VERSION command support. #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/fi/solita/clamav/ClamAVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ClamAVClient {
private static final int CHUNK_SIZE = 2048;
private static final int DEFAULT_TIMEOUT = 500;
private static final int PONG_REPLY_LEN = 4;
private static final int VERSION_REPLY_LEN = 256;

/**
* @param hostName The hostname of the server running clamav-daemon
Expand Down Expand Up @@ -66,6 +67,28 @@ public boolean ping() throws IOException {
}
}

/**
* Run VERSION command to clamd.
*
* @return Version string that the server responded with.
*/
public boolean version() throws IOException {
try (Socket s = new Socket(hostName,port); OutputStream outs = s.getOutputStream()) {
s.setSoTimeout(timeout);
outs.write(asBytes("zVERSION\0"));
outs.flush();
byte[] b = new byte[VERSION_REPLY_LEN];
InputStream inputStream = s.getInputStream();
int copyIndex = 0;
int readResult;
do {
readResult = inputStream.read(b, copyIndex, Math.max(b.length - copyIndex, 0));
copyIndex += readResult;
} while (readResult > 0);
return new String(b, StandardCharsets.UTF_8);
}
}

/**
* Streams the given data to the server in chunks. The whole data is not kept in memory.
* This method is preferred if you don't want to keep the data in memory, for instance by scanning a file on disk.
Expand Down