private static void parseData() throws IOException {
    TreeSet<String> fs = scanPath();
    parseMeta();
    if (fs.size() == 0) return;
    String absolutePath = new File(fs.first()).getCanonicalPath();
    String absolutePath2 = new File(name).getCanonicalPath();
    if (!absolutePath.equals(absolutePath2)) {
      System.out.println("meta file name not match first file, use first file");
      pos = 0;
    }

    for (String filename : fs) {
      RandomAccessFile f = new RandomAccessFile(filename, "r");
      MappedByteBuffer map = f.getChannel().map(MapMode.READ_ONLY, 0, f.length());
      map.position((int) pos);
      while (map.hasRemaining()) {
        int size = map.getInt();
        byte[] c = new byte[size];
        map.get(c);
        Message m = MessageFactory.getInstance().createMessageFrom(c);
        m.decompress();
        System.out.println("get content: " + asString(m));
      }
      map.clear();
      map = null;
      f.close();
      pos = 0;
    }
  }
示例#2
0
  /**
   * Seek for box with the specified id starting from the current location of filepointer,
   *
   * <p>Note it wont find the box if it is contained with a level below the current level, nor if we
   * are at a parent atom that also contains data and we havent yet processed the data. It will work
   * if we are at the start of a child box even if it not the required box as long as the box we are
   * looking for is the same level (or the level above in some cases).
   *
   * @param raf
   * @param id
   * @throws java.io.IOException
   */
  public static Mp4BoxHeader seekWithinLevel(RandomAccessFile raf, String id) throws IOException {
    logger.finer("Started searching for:" + id + " in file at:" + raf.getChannel().position());

    Mp4BoxHeader boxHeader = new Mp4BoxHeader();
    ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_LENGTH);
    int bytesRead = raf.getChannel().read(headerBuffer);
    if (bytesRead != HEADER_LENGTH) {
      return null;
    }
    headerBuffer.rewind();
    boxHeader.update(headerBuffer);
    while (!boxHeader.getId().equals(id)) {
      logger.finer("Still searching for:" + id + " in file at:" + raf.getChannel().position());

      // Something gone wrong probably not at the start of an atom so return null;
      if (boxHeader.getLength() < Mp4BoxHeader.HEADER_LENGTH) {
        return null;
      }
      int noOfBytesSkipped = raf.skipBytes(boxHeader.getDataLength());
      logger.finer("Skipped:" + noOfBytesSkipped);
      if (noOfBytesSkipped < boxHeader.getDataLength()) {
        return null;
      }
      headerBuffer.rewind();
      bytesRead = raf.getChannel().read(headerBuffer);
      logger.finer("Header Bytes Read:" + bytesRead);
      headerBuffer.rewind();
      if (bytesRead == Mp4BoxHeader.HEADER_LENGTH) {
        boxHeader.update(headerBuffer);
      } else {
        return null;
      }
    }
    return boxHeader;
  }
示例#3
0
 public void loadMetaData() throws IOException {
   pageFile.seek(offset);
   nextPageId = pageFile.readInt();
   currentFill = pageFile.readInt();
   bloomfilter = pageFile.readInt();
   type = pageFile.readByte();
 }
示例#4
0
 public static String readString(RandomAccessFile f, long l) throws IOException {
   f.seek(l - 1);
   int length = (int) f.readUnsignedByte();
   byte[] strArr = new byte[length];
   f.read(strArr);
   return new String(strArr);
 }
示例#5
0
 /**
  * Sets the version for the given neostore file in {@code storeDir}.
  *
  * @param storeDir the store dir to locate the neostore file in.
  * @param version the version to set.
  * @return the previous version before writing.
  */
 public static long setVersion(String storeDir, long version) {
   RandomAccessFile file = null;
   try {
     file = new RandomAccessFile(new File(storeDir, NeoStore.DEFAULT_NAME), "rw");
     FileChannel channel = file.getChannel();
     channel.position(RECORD_SIZE * 2 + 1 /*inUse*/);
     ByteBuffer buffer = ByteBuffer.allocate(8);
     channel.read(buffer);
     buffer.flip();
     long previous = buffer.getLong();
     channel.position(RECORD_SIZE * 2 + 1 /*inUse*/);
     buffer.clear();
     buffer.putLong(version).flip();
     channel.write(buffer);
     return previous;
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     try {
       if (file != null) file.close();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 }
示例#6
0
  /**
   * @param filePath
   * @param seek
   * @param length
   * @return
   */
  public static byte[] readFlieToByte(String filePath, int seek, int length) {
    if (TextUtils.isEmpty(filePath)) {
      return null;
    }
    File file = new File(filePath);
    if (!file.exists()) {
      return null;
    }
    if (length == -1) {
      length = (int) file.length();
    }

    try {
      RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
      byte[] bs = new byte[length];
      randomAccessFile.seek(seek);
      randomAccessFile.readFully(bs);
      randomAccessFile.close();
      return bs;
    } catch (Exception e) {
      e.printStackTrace();
      LogUtil.e(
          LogUtil.getLogUtilsTag(FileUtils.class), "readFromFile : errMsg = " + e.getMessage());
      return null;
    }
  }
示例#7
0
 public String getValueFromFile(long nextOffset, RandomAccessFile dataFile) throws IOException {
   int beginPtr = (int) dataFile.getFilePointer();
   byte[] res = new byte[(int) (nextOffset - beginPtr)];
   dataFile.read(res);
   String result = new String(res, StandardCharsets.UTF_8);
   return result;
 }
 private String readLine() throws IOException {
   StringBuffer sb = new StringBuffer();
   char readChar;
   int ch;
   long pos = reader.getFilePointer();
   long length = file.length();
   if ((length < pos) || (length == pos && FileUtils.isFileNewer(file, accessTime))) {
     // file got rotated or truncated
     reader.close();
     reader = new RandomAccessFile(file, "r");
     position = 0;
     reader.seek(position);
     pos = 0;
   }
   accessTime = System.currentTimeMillis();
   while ((ch = reader.read()) != -1) {
     readChar = (char) ch;
     if (readChar != delimiter) {
       sb.append(readChar);
     } else {
       return sb.toString();
     }
   }
   reader.seek(pos);
   return null;
 }
示例#9
0
  private int readBytes(Entry entry, byte[] buffer) throws IOException {
    byte[] header = getHeader(entry);

    // entry is not compressed?
    if (get2ByteLittleEndian(header, 8) == 0) {
      zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
      int offset = 0;
      int size = buffer.length;
      while (offset < size) {
        int count = zipRandomFile.read(buffer, offset, size - offset);
        if (count == -1) break;
        offset += count;
      }
      return entry.size;
    }

    int csize = entry.compressedSize;
    byte[] cbuf = new byte[csize];
    zipRandomFile.skipBytes(get2ByteLittleEndian(header, 26) + get2ByteLittleEndian(header, 28));
    zipRandomFile.readFully(cbuf, 0, csize);

    int count = inflate(cbuf, buffer);
    if (count == -1) throw new ZipException("corrupted zip file");

    return entry.size;
  }
示例#10
0
  public void newCheckpoint(byte[] state, byte[] stateHash, int consensusId) {
    String ckpPath = DEFAULT_DIR + String.valueOf(id) + "." + System.currentTimeMillis() + ".tmp";
    try {
      checkpointLock.lock();
      RandomAccessFile ckp = new RandomAccessFile(ckpPath, (syncCkp ? "rwd" : "rw"));

      ByteBuffer bf = ByteBuffer.allocate(state.length + stateHash.length + 4 * INT_BYTE_SIZE);
      bf.putInt(state.length);
      bf.put(state);
      bf.putInt(stateHash.length);
      bf.put(stateHash);
      bf.putInt(EOF);
      bf.putInt(consensusId);

      byte[] ckpState = bf.array();

      ckp.write(ckpState);
      ckp.close();

      if (isToLog) deleteLogFile();
      deleteLastCkp();
      renameCkp(ckpPath);
      if (isToLog) createLogFile();

    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      checkpointLock.unlock();
    }
  }
示例#11
0
 void corruptFile(int position) throws Exception {
   File file = new File(getDirectory(), "transaction.log");
   RandomAccessFile ra = new RandomAccessFile(file, "rw");
   ra.seek(position);
   ra.write(0xff);
   ra.close();
 }
示例#12
0
 private void addDescriptions() throws IOException {
   RandomAccessFile f = new RandomAccessFile("randomDescription.csv", "r");
   String dataString = null;
   Random random = new Random(1l);
   ArrayList<String> descriptions = new ArrayList<String>();
   while ((dataString = f.readLine()) != null) {
     descriptions.add(dataString);
   }
   f.close();
   try {
     ArrayList<Resource> resources = resourceDAO.getAllResources();
     for (Resource r : resources) {
       r.setDescription(descriptions.get(random.nextInt(descriptions.size())));
       resourceDAO.updateResource(r);
     }
   } catch (DAOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (ResourceNameExistsException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (ResourceHasActiveProjectException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
示例#13
0
 private void addResources() throws IOException {
   RandomAccessFile f = new RandomAccessFile("randomResource.csv", "r");
   String dataString = null;
   ResourceType type = null;
   while ((dataString = f.readLine()) != null) {
     String[] data = dataString.split(";");
     Resource insert = new Resource();
     if (data[0].length() < 45) {
       insert.setResourceName(data[0]);
     } else {
       insert.setResourceName(data[0].substring(0, 44));
     }
     try {
       insert.setResourceTypeID(
           resourceTypeDAO.getResourceTypeByResourceTypeName(data[1]).getResourceTypeID());
     } catch (Exception e1) {
       e1.printStackTrace();
     }
     insert.setDescription("");
     insert.setActive(true);
     System.out.println(insert);
     try {
       resourceDAO.insertResource(insert);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   f.close();
 }
示例#14
0
    @Override
    public void run() {
      try {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty(
            "Accept",
            "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                + "application/x-shockwave-flash, application/xaml+xml, "
                + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                + "application/x-ms-application, application/vnd.ms-excel, "
                + "application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");

        InputStream inputStream = conn.getInputStream();

        // 跳过startPos那一部分内容,表明该线程仅下载属于它自己的那一部分
        inputStream.skip(this.startPos);

        byte[] bytes = new byte[1024];
        int hasRead = 0;
        while (length < currentPartSize && (hasRead = inputStream.read(bytes)) != -1) {
          currentPart.write(bytes, 0, hasRead);
          length += hasRead;
        }
        currentPart.close();
        inputStream.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  /**
   * Reads data from the given file into the given buffer, centered around the given file offset.
   * The first half of the buffer will be filled with data right before the given offset, while the
   * remainder of the buffer will contain data right after it (of course, containing the byte at the
   * given offset).
   *
   * @param stream The stream to read from
   * @param buffer The buffer to read data into
   * @param fileReferenceOffset The offset to start reading from in the stream.
   * @return The number of bytes reads, which could be less than the length of the input buffer if
   *     we can't read due to the beginning or the end of the file.
   * @throws IOException Thrown if the stream being used is invalid or inaccessible.
   */
  private static int readIntoBufferAroundReference(
      RandomAccessFile stream, byte[] buffer, long fileReferenceOffset) throws IOException {
    int length = buffer.length;
    // calculate start offset
    long fileStartOffset = fileReferenceOffset - length / 2;

    if (fileStartOffset < 0) {
      // offset is less than zero, adjust it, as well as the length we want to read
      length += (int) fileStartOffset;
      fileStartOffset = 0;
      if (length <= 0) {
        return 0;
      }
    }

    if (fileStartOffset + length > stream.length()) {
      // startOffset + length is beyond the end of the stream, adjust the length accordingly
      length = (int) (stream.length() - fileStartOffset);
      if (length <= 0) {
        return 0;
      }
    }

    // read the appropriate block of the file into the buffer, using symmetry with respect to its
    // midpoint
    // we always initiate a seek from the origin of the file.
    stream.seek(0);
    stream.seek(fileStartOffset);
    int bufferOffset = 0;
    while (bufferOffset < length) {
      int bytesRead = stream.read(buffer, bufferOffset, length - bufferOffset);
      bufferOffset += bytesRead;
    }
    return length;
  }
示例#16
0
 /** Creates new CoverageFormat1 */
 protected CoverageFormat1(RandomAccessFile raf) throws IOException {
   glyphCount = raf.readUnsignedShort();
   glyphIds = new int[glyphCount];
   for (int i = 0; i < glyphCount; i++) {
     glyphIds[i] = raf.readUnsignedShort();
   }
 }
示例#17
0
  protected ChannelFuture sendFile(File file) {
    final RandomAccessFile raf;
    try {
      raf = new RandomAccessFile(file, "r");
      long fileLength = file.length();

      // Write the content.
      ChannelFuture writeFuture;
      if (isSSL()) {
        // Cannot use zero-copy with HTTPS.
        writeFuture = channel.write(new ChunkedFile(raf, 0, fileLength, 8192));
      } else {
        // No encryption - use zero-copy.
        final FileRegion region = new DefaultFileRegion(raf.getChannel(), 0, fileLength);
        writeFuture = channel.write(region);
      }
      writeFuture.addListener(
          new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) throws Exception {
              raf.close();
            }
          });
      return writeFuture;
    } catch (IOException e) {
      handleException(e);
      return null;
    }
  }
示例#18
0
 private final void testVerifyUnversioned(String subDir) throws Exception {
   UnversionedVerifier verifier = new UnversionedVerifier(testData(subDir));
   RandomAccessFile activeInput = new RandomAccessFile(testData(subDir) + "/2.unversioned", "r");
   String activeSignature = activeInput.readLine();
   activeInput.close();
   assertTrue(verifier.verify(input, activeSignature));
 }
 @Override
 protected void parseBody(final RandomAccessFile mpegFile) throws IOException {
   final int size = (int) (super.seekToBodyStart(mpegFile) - 4);
   mpegFile.skipBytes(4); // Reserved
   rawBytes = new byte[size];
   mpegFile.readFully(rawBytes);
 }
示例#20
0
  @SuppressWarnings({"unchecked"})
  void initializeFS() {
    try {
      JSONObject obj = new JSONObject();
      JSONArray jar = new JSONArray();
      jar.add("0 1#1");
      obj.put("version", jar);
      obj.put("lastblock", "1");
      version = 0;
      file.write(obj.toJSONString().getBytes());

      superBlock = new SuperBlock(obj.toJSONString().getBytes());
      // Done super block
      lastblock = 1;
      file.seek(1024);
      // root direc
      JSONObject obj1 = new JSONObject();
      JSONArray ch_dir = new JSONArray();
      obj1.put("dir", ch_dir);
      file.write(obj1.toJSONString().getBytes());
      file.close();

      getFile();
      superBlock.loadDMap(version, file);

    } catch (IOException | ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void getAgentIcons() {
    try {
      RandomAccessFile participantsFile =
          new RandomAccessFile(
              Simulator.inputFoldersPath + Simulator.inputFolder + "/participants.csv", "r");
      participantsFile.readLine();

      String currentLine = participantsFile.readLine();

      // Set the System Data
      while (!(currentLine == null)) {
        String[] theArgs = StringParseTools.readTokens(currentLine, ",");

        if (theArgs[2].contains("<player>")) {
          try {
            agentIcons.put(theArgs[1], convert(ImageIO.read(new File("images/" + theArgs[3]))));
          } catch (Exception e) {
            logger.warn(
                "Couldn't find the icon file 'images/"
                    + theArgs[3]
                    + "' for agent '"
                    + theArgs[1]
                    + "'.");
          }
        }

        // get next line
        currentLine = participantsFile.readLine();
      }
      participantsFile.close();
    } catch (Exception e) {
      logger.fatal("Error access System I/O file: ", e);
    }
  }
示例#22
0
文件: GIDE.java 项目: rexut/jkcemu
 private void execFormatTrackTask() {
   File file = this.ioFile;
   long pos = this.ioFilePos;
   int cnt = this.ioByteCnt;
   if ((file != null) && (pos >= 0) && (cnt > 0)) {
     boolean err = false;
     RandomAccessFile raf = null;
     try {
       raf = new RandomAccessFile(file, "rw");
       raf.seek(pos);
       while (cnt > 0) {
         raf.write(0);
         --cnt;
       }
       raf.close();
       raf = null;
     } catch (IOException ex) {
       err = true;
       if (!this.writeErrShown) {
         this.writeErrShown = true;
         EmuUtil.fireShowError(this.owner, null, ex);
       }
     } finally {
       EmuUtil.doClose(raf);
     }
     if (err) {
       this.errorReg = ERROR_UNCORRECTABLE_DATA;
       this.statusReg |= STATUS_ERROR;
     }
     fireInterrupt();
   }
 }
示例#23
0
  @Override
  public void run() {

    try {
      while (shouldIRun) {
        Thread.sleep(crunchifyRunEveryNSeconds);
        long fileLength = crunchifyFile.length();
        if (fileLength > lastKnownPosition) {

          // Reading and writing file
          RandomAccessFile readWriteFileAccess = new RandomAccessFile(crunchifyFile, "rw");
          readWriteFileAccess.seek(lastKnownPosition);
          String crunchifyLine = null;
          while ((crunchifyLine = readWriteFileAccess.readLine()) != null) {
            for (Session s : AppLogView.sessions) {
              // s.getAsyncRemote().sendText(crunchifyLine);
              s.getBasicRemote().sendText(crunchifyLine);
            }
          }
          lastKnownPosition = readWriteFileAccess.getFilePointer();
          readWriteFileAccess.close();
        }
      }
    } catch (Exception e) {
      shouldIRun = false;
    }
  }
  /** @return A {@link Yes1Reader}, {@link Yes2Reader}, or null if there is any error. */
  public static BibleReader createYesReader(String filename) {
    try {
      RandomAccessFile f = new RandomAccessFile(filename, "r");
      byte[] header = new byte[8];
      f.read(header);

      if (header[0] != (byte) 0x98
          || header[1] != (byte) 0x58
          || header[2] != (byte) 0x0d
          || header[3] != (byte) 0x0a
          || header[4] != (byte) 0x00
          || header[5] != (byte) 0x5d
          || header[6] != (byte) 0xe0) {
        Log.e(TAG, "Yes file has not a correct header. Header is: " + Arrays.toString(header));
        return null;
      }

      if (header[7] == 0x01) { // VERSION 1 YES
        return new Yes1Reader(f);
      } else if (header[7] == 0x02) { // VERSION 2 YES
        return new Yes2Reader(new RandomAccessFileRandomInputStream(f));
      } else {
        Log.e(TAG, "Yes file version unsupported: " + header[7]);
        return null;
      }
    } catch (IOException e) {
      Log.e(TAG, "@@createYesReader io exception", e);
      return null;
    }
  }
示例#25
0
 private static long getRecord(String storeDir, long recordPosition) {
   RandomAccessFile file = null;
   try {
     file = new RandomAccessFile(new File(storeDir), "rw");
     FileChannel channel = file.getChannel();
     /*
      * We have to check size, because the store version
      * field was introduced with 1.5, so if there is a non-clean
      * shutdown we may have a buffer underflow.
      */
     if (recordPosition > 3 && channel.size() < RECORD_SIZE * 5) {
       return -1;
     }
     channel.position(RECORD_SIZE * recordPosition + 1 /*inUse*/);
     ByteBuffer buffer = ByteBuffer.allocate(8);
     channel.read(buffer);
     buffer.flip();
     long previous = buffer.getLong();
     return previous;
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     try {
       if (file != null) file.close();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 }
示例#26
0
 private String readInstallationFileBad(File installation) throws IOException {
   RandomAccessFile f = new RandomAccessFile(installation, "r");
   byte[] bytes = new byte[(int) f.length()];
   f.readFully(bytes);
   f.close();
   return new String(bytes);
 }
示例#27
0
 public void saveMetaData() throws IOException {
   pageFile.seek(offset);
   pageFile.writeInt(nextPageId);
   pageFile.writeInt(currentFill);
   pageFile.writeInt(bloomfilter);
   pageFile.writeByte(type);
 }
示例#28
0
 public int luaCB_read(Lua l, LuaPlugin plugin) {
   l.pushNil();
   l.pushNil();
   try {
     long offset = (long) l.checkNumber(2);
     int length = (int) l.checkNumber(3);
     if (length <= 0) {
       l.pushNil();
       l.pushString("Length is not positive");
       return 2;
     }
     byte[] tmp = new byte[length];
     object.seek(offset);
     length = object.read(tmp);
     if (length < 0) {
       l.pushNil();
       l.pushNil();
       return 2;
     }
     StringBuffer buf = new StringBuffer(length);
     for (byte x : tmp) buf.appendCodePoint((int) x & 0xFF);
     l.push(buf.toString());
   } catch (IOException e) {
     l.pushNil();
     l.pushString("IOException: " + e.getMessage());
     return 2;
   }
   return 1;
 }
示例#29
0
  /**
   * Parses the zip64 end of central directory record locator. The locator must be placed
   * immediately before the end of central directory (eocd) record starting at {@code eocdOffset}.
   *
   * <p>The position of the file cursor for {@code raf} after a call to this method is undefined an
   * callers must reposition it after each call to this method.
   */
  public static long parseZip64EocdRecordLocator(RandomAccessFile raf, long eocdOffset)
      throws IOException {
    // The spec stays curiously silent about whether a zip file with an EOCD record,
    // a zip64 locator and a zip64 eocd record is considered "empty". In our implementation,
    // we parse all records and read the counts from them instead of drawing any size or
    // layout based information.
    if (eocdOffset > ZIP64_LOCATOR_SIZE) {
      raf.seek(eocdOffset - ZIP64_LOCATOR_SIZE);
      if (Integer.reverseBytes(raf.readInt()) == ZIP64_LOCATOR_SIGNATURE) {
        byte[] zip64EocdLocator = new byte[ZIP64_LOCATOR_SIZE - 4];
        raf.readFully(zip64EocdLocator);
        ByteBuffer buf = ByteBuffer.wrap(zip64EocdLocator).order(ByteOrder.LITTLE_ENDIAN);

        final int diskWithCentralDir = buf.getInt();
        final long zip64EocdRecordOffset = buf.getLong();
        final int numDisks = buf.getInt();

        if (numDisks != 1 || diskWithCentralDir != 0) {
          throw new ZipException("Spanned archives not supported");
        }

        return zip64EocdRecordOffset;
      }
    }

    return -1;
  }
示例#30
0
文件: CsvFile.java 项目: bcdev/beam
 private CsvFile(File csv, CoordinateReferenceSystem crs) throws IOException {
   this.csv = csv;
   ConverterRegistry.getInstance().setConverter(ProductData.UTC.class, new UTCConverter());
   this.crs = crs;
   RandomAccessFile randomAccessFile = new RandomAccessFile(csv, "r");
   stream = new FileChannelImageInputStream(randomAccessFile.getChannel());
 }