Java:Stop/Start network Connection
<p>I have a method for downloading a file from URL.I need to write unit test cases for resume download.SO is there any way to disable/enable the network connection so that resume download method can invoke.</p>

<p>My download method is as follows,</p>

<pre><code>public String download(LatestVersionInfo info) throws FileNotFoundException, XMLStreamException,NoSuchAlgorithmException, MalformedURLException {       
        boolean isCached = checkCache(info, location);
        String filePath = null;
        RandomAccessFile randomAccessFile = null;
        InputStream inputStream = null;
        if (!isCached) {
            try {
                createLocation();
                int responseCode = 0;
                HttpsURLConnection connection = getSSLCertificate(info
                        .getLatestVersionUrl().toString());
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Range", "bytes=" + downloaded
                        + "-");
                connection.connect();
                responseCode = connection.getResponseCode();
                if (responseCode / 100 != 2) {
                    error();
                }
                int contentLength = connection.getContentLength();
                if (contentLength &lt; 1) {
                    error();
                }
                if (size == -1) {
                    size = contentLength;
                    stateChanged();
                }
                randomAccessFile = new RandomAccessFile(
                        getDownLoadPath(info.getLatestVersionUrl()), "rw");
                randomAccessFile.seek(downloaded);
                inputStream = connection.getInputStream();
                while (status == DOWNLOADING) {
                    byte buffer[];
                    float finalSize = size - downloaded;
                    if (finalSize &gt; MAX_BUFFER_SIZE) {
                        buffer = new byte[(int) finalSize];
                    } else {
                        buffer = new byte[MAX_BUFFER_SIZE];
                    }
                    int read = inputStream.read(buffer);
                    if (read == -1)
                        break;

                    randomAccessFile.write(buffer, 0, read);
                    downloaded += read;
                    stateChanged();
                }

                if (status == DOWNLOADING) {
                    status = COMPLETE;
                    stateChanged();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (randomAccessFile != null)
                        randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null)
                        inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }           
        }
        return filePath;
    }
</code></pre>

<p>Is there any way to <strong>disable/enable the network connection</strong> so that resume download method can invoke.</p>

<p>Thanks</p>
