Class ClamDaemonClient


  • public class ClamDaemonClient
    extends Object
    Simple client for ClamAV's clamd scanner. Provides straightforward instream scanning. Support for basic INSTREAM scanning and PING command. Clamd protocol is explained here:

    http://linux.die.net/man/8/clamd

    • Constructor Detail

      • ClamDaemonClient

        public ClamDaemonClient​(String hostName,
                                int port,
                                int timeout,
                                int chunkSize)
        Parameters:
        hostName - The hostname of the server running clamav-daemon
        port - The port that clamav-daemon listens to(By default it might not listen to a port. Check your clamav configuration).
        timeout - zero means infinite timeout. Not a good idea, but will be accepted.
      • ClamDaemonClient

        public ClamDaemonClient​(String hostName,
                                int port)
    • Method Detail

      • ping

        public boolean ping()
                     throws IOException
        Run PING command to CLAMD to test it is responding.
        Returns:
        true if the server responded with proper ping reply.
        Throws:
        IOException - if there is an I/O problem communicating with CLAMD
      • scan

        public byte[] scan​(InputStream is)
                    throws IOException
        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. Since the parameter InputStream is not reset, you can not use the stream afterwards, as it will be left in a EOF-state. If your goal is to scan some data, and then pass that data further, consider using scan(byte[] in).

        Opens a socket and reads the reply. Parameter input stream is NOT closed.

        Parameters:
        is - data to scan. Not closed by this method!
        Returns:
        server reply
        Throws:
        IOException - if there is an I/O problem
      • scan

        public byte[] scan​(byte[] in)
                    throws IOException
        Scans bytes for virus by passing the bytes to clamav
        Parameters:
        in - data to scan.
        Returns:
        server reply
        Throws:
        IOException - if there is an I/O problem
      • getSocket

        protected Socket getSocket()
                            throws IOException
        Get a socket to the current host and port. Partly implemented as a separate method for unit testing purposes.
        Returns:
        socket to host and port
        Throws:
        IOException - if an I/O error occurs when creating the socket
      • isCleanReply

        public static boolean isCleanReply​(byte[] reply)
        Interpret the result from a ClamAV scan, and determine if the result means the data is clean.
        Parameters:
        reply - The reply from the server after scanning
        Returns:
        true if no virus was found according to the clamd reply message