From 8400bfd8c10b26f28cedd9f474a7052b0ab4d458 Mon Sep 17 00:00:00 2001 From: Tommi Kyntola Date: Tue, 14 Nov 2023 10:10:28 +0200 Subject: [PATCH] Add VERSION command support. --- .../java/fi/solita/clamav/ClamAVClient.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/fi/solita/clamav/ClamAVClient.java b/src/main/java/fi/solita/clamav/ClamAVClient.java index 7f14e11..c1259ed 100644 --- a/src/main/java/fi/solita/clamav/ClamAVClient.java +++ b/src/main/java/fi/solita/clamav/ClamAVClient.java @@ -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 @@ -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.