class PathURLConnection extends URLConnection {

  /*
   * This implementation should be able to work for any proper file system implementation... it
   * might be useful to release it and make it usable by other file systems.
   */

  private static final String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss \'GMT\'";
  private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";

  private InputStream stream;
  private ImmutableListMultimap<String, String> headers = ImmutableListMultimap.of();

  PathURLConnection(URL url) {
    super(checkNotNull(url));
  }

  @Override
  public void connect() throws IOException {
    if (stream != null) {
      return;
    }

    Path path = Paths.get(toUri(url));

    long length;
    if (Files.isDirectory(path)) {
      // Match File URL behavior for directories by having the stream contain the filenames in
      // the directory separated by newlines.
      StringBuilder builder = new StringBuilder();
      try (DirectoryStream<Path> files = Files.newDirectoryStream(path)) {
        for (Path file : files) {
          builder.append(file.getFileName()).append('\n');
        }
      }
      byte[] bytes = builder.toString().getBytes(UTF_8);
      stream = new ByteArrayInputStream(bytes);
      length = bytes.length;
    } else {
      stream = Files.newInputStream(path);
      length = Files.size(path);
    }

    FileTime lastModified = Files.getLastModifiedTime(path);
    String contentType =
        MoreObjects.firstNonNull(Files.probeContentType(path), DEFAULT_CONTENT_TYPE);

    ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder();
    builder.put("content-length", "" + length);
    builder.put("content-type", contentType);
    if (lastModified != null) {
      DateFormat format = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
      format.setTimeZone(TimeZone.getTimeZone("GMT"));
      builder.put("last-modified", format.format(new Date(lastModified.toMillis())));
    }

    headers = builder.build();
  }

  private static URI toUri(URL url) throws IOException {<FILL_FUNCTION_BODY>}

  @Override
  public InputStream getInputStream() throws IOException {
    connect();
    return stream;
  }

  @SuppressWarnings("unchecked") // safe by specification of ListMultimap.asMap()
  @Override
  public Map<String, List<String>> getHeaderFields() {
    try {
      connect();
    } catch (IOException e) {
      return ImmutableMap.of();
    }
    return (ImmutableMap<String, List<String>>) (ImmutableMap<String, ?>) headers.asMap();
  }

  @Override
  public @Nullable String getHeaderField(String name) {
    try {
      connect();
    } catch (IOException e) {
      return null;
    }

    // no header should have more than one value
    return Iterables.getFirst(headers.get(Ascii.toLowerCase(name)), null);
  }
}

    try {
      return url.toURI();
    } catch (URISyntaxException e) {
      throw new IOException("URL " + url + " cannot be converted to a URI", e);
    }
  
<methods>public void addRequestProperty(java.lang.String, java.lang.String) ,public abstract void connect() throws java.io.IOException,public boolean getAllowUserInteraction() ,public int getConnectTimeout() ,public java.lang.Object getContent() throws java.io.IOException,public java.lang.Object getContent(Class<?>[]) throws java.io.IOException,public java.lang.String getContentEncoding() ,public int getContentLength() ,public long getContentLengthLong() ,public java.lang.String getContentType() ,public long getDate() ,public static boolean getDefaultAllowUserInteraction() ,public static java.lang.String getDefaultRequestProperty(java.lang.String) ,public boolean getDefaultUseCaches() ,public static boolean getDefaultUseCaches(java.lang.String) ,public boolean getDoInput() ,public boolean getDoOutput() ,public long getExpiration() ,public static java.net.FileNameMap getFileNameMap() ,public java.lang.String getHeaderField(java.lang.String) ,public java.lang.String getHeaderField(int) ,public long getHeaderFieldDate(java.lang.String, long) ,public int getHeaderFieldInt(java.lang.String, int) ,public java.lang.String getHeaderFieldKey(int) ,public long getHeaderFieldLong(java.lang.String, long) ,public Map<java.lang.String,List<java.lang.String>> getHeaderFields() ,public long getIfModifiedSince() ,public java.io.InputStream getInputStream() throws java.io.IOException,public long getLastModified() ,public java.io.OutputStream getOutputStream() throws java.io.IOException,public java.security.Permission getPermission() throws java.io.IOException,public int getReadTimeout() ,public Map<java.lang.String,List<java.lang.String>> getRequestProperties() ,public java.lang.String getRequestProperty(java.lang.String) ,public java.net.URL getURL() ,public boolean getUseCaches() ,public static java.lang.String guessContentTypeFromName(java.lang.String) ,public static java.lang.String guessContentTypeFromStream(java.io.InputStream) throws java.io.IOException,public void setAllowUserInteraction(boolean) ,public void setConnectTimeout(int) ,public static synchronized void setContentHandlerFactory(java.net.ContentHandlerFactory) ,public static void setDefaultAllowUserInteraction(boolean) ,public static void setDefaultRequestProperty(java.lang.String, java.lang.String) ,public void setDefaultUseCaches(boolean) ,public static void setDefaultUseCaches(java.lang.String, boolean) ,public void setDoInput(boolean) ,public void setDoOutput(boolean) ,public static void setFileNameMap(java.net.FileNameMap) ,public void setIfModifiedSince(long) ,public void setReadTimeout(int) ,public void setRequestProperty(java.lang.String, java.lang.String) ,public void setUseCaches(boolean) ,public java.lang.String toString() <variables>static final boolean $assertionsDisabled,protected boolean allowUserInteraction,private int connectTimeout,protected boolean connected,private static final java.lang.String contentClassPrefix,private static final java.lang.String contentPathProp,private static boolean defaultAllowUserInteraction,private static final ConcurrentHashMap<java.lang.String,java.lang.Boolean> defaultCaching,private static volatile boolean defaultUseCaches,protected boolean doInput,protected boolean doOutput,private static volatile java.net.ContentHandlerFactory factory,private static volatile java.net.FileNameMap fileNameMap,private static final Hashtable<java.lang.String,java.net.ContentHandler> handlers,protected long ifModifiedSince,private int readTimeout,private sun.net.www.MessageHeader requests,protected java.net.URL url,protected boolean useCaches