@Override
 public LocalFilesystemURL toLocalUri(Uri inputURL) {
   if (!"file".equals(inputURL.getScheme())) {
     return null;
   }
   File f = new File(inputURL.getPath());
   // Removes and duplicate /s (e.g. file:///a//b/c)
   Uri resolvedUri = Uri.fromFile(f);
   String rootUriNoTrailingSlash = rootUri.getEncodedPath();
   rootUriNoTrailingSlash =
       rootUriNoTrailingSlash.substring(0, rootUriNoTrailingSlash.length() - 1);
   if (!resolvedUri.getEncodedPath().startsWith(rootUriNoTrailingSlash)) {
     return null;
   }
   String subPath = resolvedUri.getEncodedPath().substring(rootUriNoTrailingSlash.length());
   // Strip leading slash
   if (!subPath.isEmpty()) {
     subPath = subPath.substring(1);
   }
   Uri.Builder b =
       new Uri.Builder()
           .scheme(LocalFilesystemURL.FILESYSTEM_PROTOCOL)
           .authority("localhost")
           .path(name);
   if (!subPath.isEmpty()) {
     b.appendEncodedPath(subPath);
   }
   if (isDirectory(subPath) || inputURL.getPath().endsWith("/")) {
     // Add trailing / for directories.
     b.appendEncodedPath("");
   }
   return LocalFilesystemURL.parse(b.build());
 }
예제 #2
0
  @Override
  public long writeToFileAtURL(
      LocalFilesystemURL inputURL, String data, int offset, boolean isBinary)
      throws IOException, NoModificationAllowedException {

    boolean append = false;
    if (offset > 0) {
      this.truncateFileAtURL(inputURL, offset);
      append = true;
    }

    byte[] rawData;
    if (isBinary) {
      rawData = Base64.decode(data, Base64.DEFAULT);
    } else {
      rawData = data.getBytes();
    }
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    try {
      byte buff[] = new byte[rawData.length];
      FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
      try {
        in.read(buff, 0, buff.length);
        out.write(buff, 0, rawData.length);
        out.flush();
      } finally {
        // Always close the output
        out.close();
      }
      broadcastNewFile(inputURL);
    } catch (NullPointerException e) {
      // This is a bug in the Android implementation of the Java Stack
      NoModificationAllowedException realException =
          new NoModificationAllowedException(inputURL.toString());
      throw realException;
    }

    return rawData.length;
  }