title: HttpServer - HttpExchange - Seekable Stream
body: <p>I work on a sample java http server and a .Net client (on tablet).
  using my http sever, the .Net client must be able to download files.</p>
  
  <p>It's working perfectly, but now I have to be able to resume download after a connection disruption.</p>
  
  <p>Here some code :</p>
  
  <p><strong>Java server :</strong> ( It is launched in a seperate thread, hence the run method).</p>
  
  <pre><code>public void run() {
  
      try {
          server = com.sun.net.httpserver.HttpServer.create(
                  new InetSocketAddress(
                          portNumber), this.maximumConnexion);
  
          server.setExecutor(executor);
          server.createContext("/", new ConnectionHandler(this.rootPath));
          server.start();
  
  
      } catch (IOException e1) {
          //For debugging
          e1.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p><strong>my HttpHandler :</strong> (only the part dealing with GET request)</p>
  
  <pre><code>/**
   * handleGetMethod : handle GET request. If the file specified in the URI is
   * available, send it to the client.
   * 
   * @param httpExchange
   * @throws IOException
   */
  private void handleGetMethod(HttpExchange httpExchange) throws IOException {
  
  File file = new File(this.rootPath + this.fileRef).getCanonicalFile();
  
  if (!file.isFile()) {
      this.handleError(httpExchange, 404);
  } else if (!file.getPath().startsWith(this.rootPath.replace('/', '\\'))) { // windows work with anti-slash!
      // Suspected path traversal attack.
      System.out.println(file.getPath());
      this.handleError(httpExchange, 403);
  } else {
      //Send the document.
  
  
      httpExchange.sendResponseHeaders(200, file.length());       
      System.out.println("file length : "+ file.length() + " bytes.");
  
  
      OutputStream os = httpExchange.getResponseBody();
  
      FileInputStream fs = new FileInputStream(file);
  
      final byte[] buffer = new byte[1024];
      int count = 0;
      while ((count = fs.read(buffer)) &gt;= 0) {
          os.write(buffer, 0, count);
      }
      os.flush();
      fs.close();
      os.close();
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>And now my .Net Client:</strong> (simplified)</p>
  
  <pre><code>  try{
  
          Stream response = await httpClient.GetStreamAsync(URI + this.fileToDownload.Text);
  
  
      FileSavePicker savePicker = new FileSavePicker();
      savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
      // Dropdown of file types the user can save the file as
      savePicker.FileTypeChoices.Add("Application/pdf", new List&lt;string&gt;() { ".pdf" });
      // Default file name if the user does not type one in or select a file to replace
      savePicker.SuggestedFileName = "new doc";
  
      StorageFile file = await savePicker.PickSaveFileAsync();
  
      if (file != null)
      {
  
          const int BUFFER_SIZE = 1024*1024;
          using (Stream outputFileStream = await file.OpenStreamForWriteAsync())
          {
              using (response)
              {
                  var buffer = new byte[BUFFER_SIZE];
                  int bytesRead;                          
                  do
                  {
                      bytesRead = response.Read(buffer, 0, BUFFER_SIZE);
                      outputFileStream.Write(buffer, 0, bytesRead);                                
                  } while (bytesRead &gt; 0);
              } 
              outputFileStream.Flush();
         }
  
     }
  
  }
  catch (HttpRequestException hre)
  {   //For debugging
      this.Display.Text += hre.Message;
      this.Display.Text += hre.Source;
  }
  catch (Exception ex)
  {
      //For debugging
      this.Display.Text += ex.Message;
      this.Display.Text += ex.Source;
  }
  </code></pre>
  
  <p>So, to resume the download I would like to use some seek operation within the .Net client part.
  But every time I try something like <code>response.Seek(offset, response.Position);</code> , an error occurs informing that the Stream does not support seek operations. 
  Yes, It does not, but how I can specify (in my server side) to use seekable Stream?
  Does the method HttpExchange.setStreams can be useful? 
  Or, I do not need to modify the stream but to configure my HttpServer instance?</p>
  
  <p>Thanks.</p>