public void testGetFileName() {
   try {
     FileAccessor fe = null;
     fe = FileSystem.open("file:///test0.txt");
     fe.close();
     assertEquals("flat file", "test0.txt", fe.getName());
     fe = FileSystem.open("file:///test/test1.txt");
     fe.close();
     assertEquals("dir file", "test1.txt", fe.getName());
     fe = FileSystem.open("file:///test/dir");
     fe.close();
     assertEquals("dir not end with slash", "dir", fe.getName());
     fe = FileSystem.open("file:///test/");
     fe.close();
     assertEquals("dir end with slash", "test", fe.getName());
     fe = FileSystem.open("file:///");
     fe.close();
     assertEquals("root dir", "", fe.getName());
     try {
       fe = FileSystem.open("file://");
       fail("detect root slash");
       fe.close();
     } catch (IOException e) {
     }
     try {
       fe = FileSystem.open("file://partition/test0.txt");
       fail("detect ghost partition...");
       fe.close();
     } catch (IOException ioe) {
     }
   } catch (Exception e) {
     fail(e.toString());
   }
 }
 public void testRename() {
   try {
     FileAccessor fe = null;
     fe = FileSystem.open("file:///test0.txt");
     fe.create();
     assertEquals("before write : size = 0", 0L, fe.fileSize());
     OutputStream os = fe.openOutputStream();
     byte[] data = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04};
     os.write(data);
     try {
       fe.rename("rename0.jpg");
       fail("unable to rename while writing");
     } catch (IOException e) {
     }
     os.close();
     fe.rename("rename0.jpg");
     assertEquals("after rename : name = rename0.jpg", "rename0.jpg", fe.getName());
     fe.close();
     fe = FileSystem.open("file:///test0.txt");
     assertTrue("test0.txt not exists", !fe.exsists());
     fe.close();
     fe = FileSystem.open("file:///rename0.jpg");
     assertTrue("ename0.jpg exists", fe.exsists());
     InputStream is = fe.openInputStream();
     byte[] buf = new byte[5];
     is.read(buf);
     assertSame("read data", data, buf);
     assertTrue("eof", is.read() == -1);
     is.close();
     fe.close();
   } catch (Exception e) {
     fail(e.toString());
   }
 }
예제 #3
0
  public QTFastStartRAF(FileAccessor accessor, boolean enable) throws IOException {

    input = accessor;

    if (enable) {

      String name = accessor.getName();

      boolean log;
      String fail = null;

      synchronized (tested) {
        log = !tested.contains(name);

        if (log) {

          tested.add(name);
        }
      }

      try {
        Atom ah = null;
        Atom ftypAtom = null;

        boolean gotFtyp = false;
        boolean gotMdat = false;
        boolean justCopy = false;

        while (input.getFilePointer() < input.length()) {

          ah = new Atom(input);

          // System.out.println( "got " + ah.type +", size=" + ah.size );

          if (!isValidTopLevelAtom(ah)) {
            throw new IOException("Non top level QT atom found (" + ah.type + "). File invalid?");
          }

          if (gotFtyp && !gotMdat && ah.type.equalsIgnoreCase(ATOM_MOOV)) {
            justCopy = true;
            break;
          }

          // store ftyp atom to buffer
          if (ah.type.equalsIgnoreCase(ATOM_FTYP)) {
            ftypAtom = ah;
            ftypAtom.fillBuffer(input);
            gotFtyp = true;
          } else if (ah.type.equalsIgnoreCase(ATOM_MDAT)) {
            gotMdat = true;
            input.skipBytes((int) ah.size);
          } else {
            input.skipBytes((int) ah.size);
          }
        }

        if (justCopy) {

          transparent = true;

          return;
        }

        if (ftypAtom == null) {

          throw new IOException("No FTYP atom found");
        }

        if (ah == null || !ah.type.equalsIgnoreCase(ATOM_MOOV)) {

          throw new IOException("Last QT atom was not the MOOV atom.");
        }

        input.seek(ah.offset);

        Atom moovAtom = ah;

        moovAtom.fillBuffer(input);

        if (isCompressedMoovAtom(moovAtom)) {

          throw new IOException("Compressed MOOV qt atoms are not supported");
        }

        patchMoovAtom(moovAtom);

        body_start = ftypAtom.offset + ftypAtom.size;
        body_end = moovAtom.offset;

        header = new byte[ftypAtom.buffer.length + moovAtom.buffer.length];

        System.arraycopy(ftypAtom.buffer, 0, header, 0, ftypAtom.buffer.length);
        System.arraycopy(
            moovAtom.buffer, 0, header, ftypAtom.buffer.length, moovAtom.buffer.length);

        if (accessor.length() != header.length + (body_end - body_start)) {

          throw (new IOException("Inconsistent: file size has changed"));
        }

      } catch (Throwable e) {

        // e.printStackTrace();

        fail = Debug.getNestedExceptionMessage(e);

        transparent = true;

      } finally {

        input.seek(0);

        if (log) {

          String message;

          if (fail == null) {

            message = transparent ? "Not required" : "Required";

          } else {

            message = "Failed - " + fail;
          }

          Debug.outNoStack("MOOV relocation for " + accessor.getName() + ": " + message);
        }
      }
    } else {

      transparent = true;
    }
  }