示例#1
0
  public int read(byte[] buffer, int pos, int len) throws IOException {

    if (transparent) {

      return (input.read(buffer, pos, len));
    }

    // [header]
    // file [body-start -> body-end]

    long start_pos = seek_position;
    int start_len = len;

    if (seek_position < header.length) {

      int rem = (int) (header.length - seek_position);

      if (rem > len) {

        rem = len;
      }

      System.arraycopy(header, (int) seek_position, buffer, pos, rem);

      pos += rem;
      len -= rem;

      seek_position += rem;
    }

    if (len > 0) {

      long file_position = body_start + seek_position - header.length;

      long rem = body_end - file_position;

      if (len < rem) {

        rem = len;
      }

      input.seek(file_position);

      int temp = input.read(buffer, pos, (int) rem);

      pos += temp;
      len -= temp;

      seek_position += temp;
    }

    int read = start_len - len;

    seek_position = start_pos + read;

    return (read);
  }
示例#2
0
  public FileFlatField(FileAccessor accessor, CacheStrategy strategy) throws VisADException {
    super(accessor.getFunctionType(), getNullDomainSet(accessor.getFunctionType().getDomain()));

    fileAccessor = accessor;
    cacheStrategy = strategy;

    // '0' is in legal range of adaptedFlatFieldIndex,
    // but not owned by this
    adaptedFlatFieldIndex = 0;
  }
 public void testOpen() {
   try {
     FileAccessor fe = FileSystem.open("file:///test.txt");
     assertNotNull("file entry not null", fe);
     assertTrue("not file exists", !fe.exsists());
     fe.close();
   } catch (Exception e) {
     fail(e.toString());
   }
 }
 public void testWriteAndReadFileData() {
   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};
     for (int i = 0; i < 195; ++i) {
       os.write(data);
     }
     assertEquals("before flush : size = 0", 0L, fe.fileSize());
     os.flush();
     assertEquals("after flush : size = 5 * 195", 5L * 195, fe.fileSize());
     os.close();
     fe.close();
     fe = FileSystem.open("file:///test0.txt");
     InputStream is = fe.openInputStream();
     for (int i = 0; i < 195; ++i) {
       byte[] buf = new byte[5];
       is.read(buf);
       assertSame("series[" + i + "]", data, buf);
     }
     assertTrue("eof", is.read() == -1);
     is.close();
     fe.close();
   } catch (Exception e) {
     fail(e.toString());
   }
 }
示例#5
0
 public Atom(FileAccessor input) throws IOException {
   offset = input.getFilePointer();
   // get atom size
   size = input.readInt();
   // get atom type
   byte[] atomTypeFCC = new byte[4];
   input.readFully(atomTypeFCC);
   type = new String(atomTypeFCC);
   if (size == 1) {
     // 64 bit size. Read new size from body and store it
     size = input.readLong();
   }
   // skip back to atom start
   input.seek(offset);
 }
  public void jsonWrite() throws Exception {

    Album album = Album.getSimpleAlbum();

    File file = FileAccessor.getFile("jackson_album.json");
    ObjectMapper jsonMapper = new ObjectMapper();
    jsonMapper.writerWithDefaultPrettyPrinter().writeValue(file, album);

    System.out.printf("Serialization to %s done.\n", file.getPath());
  }
  public void jsonRead() throws Exception {

    File file = FileAccessor.getFile("jackson_album.json");

    ObjectMapper jsonReader = new ObjectMapper();
    Album album = jsonReader.readValue(file, Album.class);

    System.out.printf("Reading object from %s done.\n", file.getPath());
    System.out.println(album);
  }
示例#8
0
  public void seek(long pos) throws IOException {

    if (transparent) {

      input.seek(pos);

    } else {

      seek_position = pos;
    }
  }
示例#9
0
  /**
   * @param root
   * @param fileName
   * @return
   */
  public static String getMD5FileDir(String root, String fileName) {
    // FileAccessor.IMESSAGE_IMAGE + File.separator +
    // FileAccessor.getSecondLevelDirectory(fileNameMD5)+ File.separator;
    if (TextUtils.isEmpty(root)) {
      return null;
    }
    File file = new File(root);
    if (!file.exists()) {
      file.mkdirs();
    }

    File fullPath = new File(file, FileAccessor.getSecondLevelDirectory(fileName));
    if (!fullPath.exists()) {
      fullPath.mkdirs();
    }
    return fullPath.getAbsolutePath();
  }
示例#10
0
 public void fillBuffer(FileAccessor input) throws IOException {
   buffer = new byte[(int) size];
   input.readFully(buffer);
 }
示例#11
0
  public void close() throws IOException {

    input.close();
  }
示例#12
0
  public long length() throws IOException {

    return (input.length());
  }
 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());
   }
 }
示例#15
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;
    }
  }
示例#16
0
  private FlatField getAdaptedFlatField() {
    // if owner array is null,
    //  assume this object got serialized & unserialized
    if (adaptedFlatFieldOwner == null) {
      return null;
    }

    synchronized (adaptedFlatFields) {
      for (int ii = 0; ii < MAX_FILE_FLAT_FIELDS; ii++) {
        if (this == adaptedFlatFieldOwner[ii]) {

          // mark time of most recent access

          adaptedFlatFieldTimes[ii] = System.currentTimeMillis();

          return adaptedFlatFields[ii];
        }
      }

      // this FileFlatField does not own a cache entry, so invoke
      // CahceStrategy.allocate to allocate one, possibly by taking
      // one, possibly by taking one from another FileFlatField;
      // this will be an area for lots of thought and experimentation;

      adaptedFlatFieldIndex =
          cacheStrategy.allocate(
              adaptedFlatFields, adaptedFlatFieldDirty,
              adaptedFlatFieldSizes, adaptedFlatFieldTimes);

      // flush cache entry, if dirty

      if (adaptedFlatFieldDirty[adaptedFlatFieldIndex]) {
        try {
          adaptedFlatFieldOwner[adaptedFlatFieldIndex].flushCache();
        } catch (VisADException e) {
          System.out.println(e.getMessage());
        }
      }

      // create a new entry in adaptedFlatFields at adaptedFlatFieldIndex
      // and read data values from fileAccessor at fileLocation
      try {
        adaptedFlatFields[adaptedFlatFieldIndex] = fileAccessor.getFlatField();
      } catch (VisADException e1) {
        System.out.println(e1.getMessage());
      } catch (RemoteException e2) {
        System.out.println(e2.getMessage());
      }

      // mark cache entry as belonging to this FileFlatField

      adaptedFlatFieldOwner[adaptedFlatFieldIndex] = this;

      // get size of adapted FlatField
      // (by calling a method that currently does not exist)

      /*adaptedFlatFields[adaptedFlatFieldIndex].getSize(); */

      adaptedFlatFieldTimes[adaptedFlatFieldIndex] = System.currentTimeMillis();

      return adaptedFlatFields[adaptedFlatFieldIndex];
    }
  }
 public void testCreateAndDeleteFile() {
   try {
     FileAccessor fe = null;
     fe = FileSystem.open("file:///test0.txt");
     assertTrue("before create : file not exists", !fe.exsists());
     fe.create();
     assertTrue("after create : file exists", fe.exsists());
     assertEquals("size = 0", 0L, fe.fileSize());
     fe.close();
     fe = FileSystem.open("file:///test0.txt");
     assertTrue("reopen : file exists", fe.exsists());
     assertEquals("size = 0", 0L, fe.fileSize());
     fe.delete();
     assertTrue("after delete : file not exists", !fe.exsists());
     fe.close();
     fe = FileSystem.open("file:///test0.txt");
     assertTrue("rereopen : not exists", !fe.exsists());
     fe.close();
   } catch (Exception e) {
     fail(e.toString());
   }
 }