@Override
  protected void loadFromByteBuffer(String fileName)
      throws IOException, MaryConfigurationException {
    /* Open the file */
    FileInputStream fis = new FileInputStream(fileName);
    FileChannel fc = fis.getChannel();
    ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    fis.close();

    /* Load the Mary header */
    hdr = new MaryHeader(bb);
    if (hdr.getType() != MaryHeader.HALFPHONE_UNITFEATS) {
      throw new MaryConfigurationException(
          "File [" + fileName + "] is not a valid Mary Halfphone Features file.");
    }
    leftWeights = new FeatureDefinition(bb);
    rightWeights = new FeatureDefinition(bb);
    assert leftWeights.featureEquals(rightWeights)
        : "Halfphone unit feature file contains incompatible feature definitions for left and right units -- this should not happen!";
    featureDefinition = leftWeights; // one of them, for super class
    int numberOfUnits = bb.getInt();
    featureVectors = new FeatureVector[numberOfUnits];
    for (int i = 0; i < numberOfUnits; i++) {
      featureVectors[i] = featureDefinition.readFeatureVector(i, bb);
    }
  }
Esempio n. 2
0
  /**
   * Internal copy file method.
   *
   * @param srcFile the validated source file, must not be <code>null</code>
   * @param destFile the validated destination file, must not be <code>null</code>
   * @throws IOException if an error occurs
   */
  private static void doCopyFile(File srcFile, File destFile) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
      throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
      fis = new FileInputStream(srcFile);
      fos = new FileOutputStream(destFile);
      input = fis.getChannel();
      output = fos.getChannel();
      long size = input.size();
      long pos = 0;
      long count = 0;
      while (pos < size) {
        count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
        pos += output.transferFrom(input, pos, count);
      }
    } finally {
      Streams.closeQuietly(output);
      Streams.closeQuietly(fos);
      Streams.closeQuietly(input);
      Streams.closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
      throw new IOException(
          "Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
  }
Esempio n. 3
0
  public static void main(String args[]) throws Exception {
    String inputFile = "samplein.txt";
    String outputFile = "sampleout.txt";

    RandomAccessFile inf = new RandomAccessFile(inputFile, "r");
    RandomAccessFile outf = new RandomAccessFile(outputFile, "rw");
    long inputLength = new File(inputFile).length();

    FileChannel inc = inf.getChannel();
    FileChannel outc = outf.getChannel();

    MappedByteBuffer inputData = inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);

    Charset latin1 = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = latin1.newDecoder();
    CharsetEncoder encoder = latin1.newEncoder();

    CharBuffer cb = decoder.decode(inputData);

    // Process char data here

    ByteBuffer outputData = encoder.encode(cb);

    outc.write(outputData);

    inf.close();
    outf.close();
  }
 public long getContentLength() {
   try {
     return Math.min(mChunkSize, mChannel.size() - mChannel.position());
   } catch (IOException e) {
     return mChunkSize;
   }
 }
Esempio n. 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);
     }
   }
 }
Esempio n. 6
0
  public HsErrPidList() {
    if (Functions.getIsUnitTest()) {
      return;
    }
    try {
      FileChannel ch = new FileInputStream(getSecretKeyFile()).getChannel();
      map = ch.map(MapMode.READ_ONLY, 0, 1);

      scan("./hs_err_pid%p.log");
      if (Functions.isWindows()) {
        File dir = Kernel32Utils.getTempDir();
        if (dir != null) {
          scan(dir.getPath() + "\\hs_err_pid%p.log");
        }
      } else {
        scan("/tmp/hs_err_pid%p.log");
      }
      // on different platforms, rules about the default locations are a lot more subtle.

      // check our arguments in the very end since this might fail on some platforms
      JavaVMArguments args = JavaVMArguments.current();
      for (String a : args) {
        // see http://www.oracle.com/technetwork/java/javase/felog-138657.html
        if (a.startsWith(ERROR_FILE_OPTION)) {
          scan(a.substring(ERROR_FILE_OPTION.length()));
        }
      }
    } catch (UnsupportedOperationException e) {
      // ignore
    } catch (Throwable e) {
      LOGGER.log(Level.WARNING, "Failed to list up hs_err_pid files", e);
    }
  }
  /**
   * @param directory
   * @return
   * @throws IOException
   */
  FileLock acquireLock(File directory) throws IOException {
    // Get a file channel for the file
    File file = new File(directory, LOCK_FILE);
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    // Use the file channel to create a lock on the file.
    // This method blocks until it can retrieve the lock.
    FileLock lock = channel.lock();

    // Try acquiring the lock without blocking. This method returns
    // null or throws an exception if the file is already locked.
    while (!lock.isValid()) {
      try {
        lock = channel.tryLock();
      } catch (OverlappingFileLockException e) {
        // File is already locked in this thread or virtual machine
      }
      // wait for the other process to unlock it, should take a couple of seconds
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        // someone waked us earlier, no problem
      }
    }

    return lock;
  }
Esempio n. 8
0
 /**
  * Clean up any resources. Closes the channel.
  *
  * @throws IOException If errors occur while closing the channel.
  */
 public void close() throws IOException {
   if (channel != null && channel.isOpen()) {
     channel.close();
   }
   channel = null;
   header = null;
 }
Esempio n. 9
0
 private void writeHeader(LogSegment segment, byte[] buf) throws IOException {
   FileChannel channel = segment.getView().getSecond();
   ByteBuffer buffer = ByteBuffer.wrap(buf);
   while (buffer.hasRemaining()) {
     channel.write(buffer, 0);
   }
 }
Esempio n. 10
0
  private NPOIFSFileSystem(FileChannel channel, boolean closeChannelOnError) throws IOException {
    this(false);

    try {
      // Get the header
      ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
      IOUtils.readFully(channel, headerBuffer);

      // Have the header processed
      _header = new HeaderBlock(headerBuffer);

      // Now process the various entries
      _data = new FileBackedDataSource(channel);
      readCoreContents();
    } catch (IOException e) {
      if (closeChannelOnError) {
        channel.close();
      }
      throw e;
    } catch (RuntimeException e) {
      // Comes from Iterators etc.
      // TODO Decide if we can handle these better whilst
      //  still sticking to the iterator contract
      if (closeChannelOnError) {
        channel.close();
      }
      throw e;
    }
  }
Esempio n. 11
0
 /**
  * 检查文件是否上锁
  *
  * @return
  * @throws IOException
  */
 public boolean validateFile() throws IOException {
   fileUrl = System.getProperty("user.dir") + "/SpiderRun.lock";
   File myfilelock = new File(fileUrl);
   RandomAccessFile raf = null;
   FileChannel fc = null;
   if (!myfilelock.exists()) {
     // 不存在,则新增文件,然后加锁
     raf = new RandomAccessFile(myfilelock, "rw");
     fc = raf.getChannel();
     FileLock fl = fc.tryLock();
     if (fl != null && fl.isValid()) {
       //                System.err.println(fileUrl + "文件被创建且被锁!");
       return false;
     }
   } else {
     // 存在,判断是否被锁
     raf = new RandomAccessFile(myfilelock, "rw");
     fc = raf.getChannel();
     FileLock fl = fc.tryLock();
     if (fl != null && fl.isValid()) {
       // 被锁
       System.err.println(fileUrl + "文件已被锁,请删除后,再启动该进程!");
       return true;
     }
   }
   return false;
 }
Esempio n. 12
0
 public OneInstanceGuard(String lockFileName) {
   lockFile = new File(lockFileName);
   if (lockFile.exists()) {
     lockFile.delete();
   }
   try {
     channel = new RandomAccessFile(lockFile, "rw").getChannel();
   } catch (FileNotFoundException e) {
     // Not running
     LOG.info("File not found: " + e);
     return;
   }
   try {
     lock = channel.tryLock();
     if (lock == null) {
       // File is lock by other application
       channel.close();
       throw new IOException("Instance already active");
     }
   } catch (IOException e) {
     // Running
     LOG.info("Instance already running");
     alreadyRunning = true;
     return;
   }
   ShutdownHook shutdownHook = new ShutdownHook(this);
   Runtime.getRuntime().addShutdownHook(shutdownHook);
 }
Esempio n. 13
0
    private void saveToFile(BlockLink[] links) {
      log.info("Saving matches to: " + fileName);
      try {
        File f = new File(fileName);

        if (f.exists()) {
          f.delete();
        }

        FileChannel wChannel = new RandomAccessFile(f, "rw").getChannel();
        ByteBuffer buffer = wChannel.map(FileChannel.MapMode.READ_WRITE, 0, links.length * 6 + 1);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.put(version);

        for (BlockLink link : links) {
          buffer.putShort((short) link.blockIndex);
          buffer.put(link.linkMapX);
          buffer.put(link.linkMapY);
          buffer.putShort((short) link.linkBlockIndex);
        }

        wChannel.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
Esempio n. 14
0
  public static BlockLink[] loadBlockMatches(String fileName) {
    File f = new File(fileName);
    if (!f.exists()) {
      return null;
    }
    try {
      FileChannel roChannel = new RandomAccessFile(f, "r").getChannel();

      int count = (int) ((roChannel.size() - 1) / 6);
      ByteBuffer buffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, roChannel.size());
      roChannel.close();
      buffer.order(ByteOrder.LITTLE_ENDIAN);
      if (buffer.get() != version) {
        return null;
      }

      BlockLink[] links = new BlockLink[count];
      for (int i = 0; i < links.length; i++) {
        links[i] = new BlockLink(buffer.getShort(), buffer.get(), buffer.get(), buffer.getShort());
      }

      return links;

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Esempio n. 15
0
 // Read up to 'len' bytes of Value. Value should already be persisted to
 // disk.  A racing delete can trigger a failure where we get a null return,
 // but no crash (although one could argue that a racing load&delete is a bug
 // no matter what).
 @Override
 public byte[] load(Value v) {
   long skip = 0;
   Key k = v._key;
   // Convert an arraylet chunk into a long-offset from the base file.
   if (k._kb[0] == Key.ARRAYLET_CHUNK) {
     skip = ValueArray.getChunkOffset(k); // The offset
     k = ValueArray.getArrayKey(k); // From the base file key
   }
   if (k._kb[0] == Key.DVEC) {
     skip = water.fvec.NFSFileVec.chunkOffset(k); // The offset
   }
   try {
     FileInputStream s = null;
     try {
       s = new FileInputStream(getFileForKey(k));
       FileChannel fc = s.getChannel();
       fc.position(skip);
       AutoBuffer ab = new AutoBuffer(fc, true, Value.NFS);
       byte[] b = ab.getA1(v._max);
       ab.close();
       assert v.isPersisted();
       return b;
     } finally {
       if (s != null) s.close();
     }
   } catch (IOException e) { // Broken disk / short-file???
     H2O.ignore(e);
     return null;
   }
 }
Esempio n. 16
0
 /**
  * Based on <a href="http://www.screaming-penguin.com/node/7749">Backing up your Android SQLite
  * database to the SD card</a>
  *
  * @param src
  * @param dst
  * @return true if success
  * @throws IOException
  */
 boolean copyFile(File src, File dst) throws IOException {
   long sizeIn = -1;
   long sizeCopied = 0;
   boolean ok = false;
   if (src != null && src.exists()) {
     sizeIn = src.length();
     if (!dst.createNewFile()) {
       MyLog.e(this, "New file was not created: '" + dst.getCanonicalPath() + "'");
     } else if (src.getCanonicalPath().compareTo(dst.getCanonicalPath()) == 0) {
       MyLog.d(this, "Cannot copy to itself: '" + src.getCanonicalPath() + "'");
     } else {
       FileInputStream fileInputStream = null;
       java.nio.channels.FileChannel inChannel = null;
       FileOutputStream fileOutputStream = null;
       java.nio.channels.FileChannel outChannel = null;
       try {
         fileInputStream = new FileInputStream(src);
         inChannel = fileInputStream.getChannel();
         fileOutputStream = new FileOutputStream(dst);
         outChannel = fileOutputStream.getChannel();
         sizeCopied = inChannel.transferTo(0, inChannel.size(), outChannel);
         ok = (sizeIn == sizeCopied);
       } finally {
         DbUtils.closeSilently(outChannel);
         DbUtils.closeSilently(fileOutputStream);
         DbUtils.closeSilently(inChannel);
         DbUtils.closeSilently(fileInputStream);
       }
     }
   }
   MyLog.d(this, "Copied " + sizeCopied + " bytes of " + sizeIn);
   return ok;
 }
Esempio n. 17
0
  private ByteList bufferedRead(int number) throws IOException, BadDescriptorException {
    checkReadable();
    ensureRead();

    int resultSize = 0;

    // 128K seems to be the minimum at which the stat+seek is faster than reallocation
    final int BULK_THRESHOLD = 128 * 1024;
    if (number >= BULK_THRESHOLD
        && descriptor.isSeekable()
        && descriptor.getChannel() instanceof FileChannel) {
      //
      // If it is a file channel, then we can pre-allocate the output buffer
      // to the total size of buffered + remaining bytes in file
      //
      FileChannel fileChannel = (FileChannel) descriptor.getChannel();
      resultSize =
          (int)
              Math.min(
                  fileChannel.size() - fileChannel.position() + bufferedInputBytesRemaining(),
                  number);
    } else {
      //
      // Cannot discern the total read length - allocate at least enough for the buffered data
      //
      resultSize = Math.min(bufferedInputBytesRemaining(), number);
    }

    ByteList result = new ByteList(resultSize);
    bufferedRead(result, number);
    return result;
  }
Esempio n. 18
0
  /*Function to read file */
  public byte[] getFile(String fname) throws Exception {

    /*FileInputStream and FileChannel for performance. */
    FileInputStream in = new FileInputStream(fname);
    FileChannel ch = in.getChannel();
    MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, ch.size());
    long l = (new File(fname)).length();

    /*Currently, supported max size is 20MB*/
    if (l > MAX_SIZE) {
      // errorMessage("File size too large. Max file size allowed
      // is"+(Integer.MAX_VALUE/1000)+"KB");
      return null;
    }

    byte[] barray = new byte[(int) l];
    int nGet;
    /*Read the file in to barray*/
    while (mb.hasRemaining()) {
      nGet = Math.min(mb.remaining(), Integer.MAX_VALUE);
      mb.get(barray, 0, nGet);
    }
    if (in != null) in.close();

    /*Return barray*/
    return barray;
  }
Esempio n. 19
0
  @Override
  public void get(ByteBuffer key, Result result) throws IOException {
    result.requiresBufferSize(readBufferSize);

    keyfile.get(key, result);

    if (result.isFound()) {
      ByteBuffer buffer = result.getBuffer();
      long recordFileOffset = EncodingHelper.decodeLittleEndianFixedWidthLong(buffer);

      buffer.rewind();
      buffer.limit(readBufferSize);
      recordFile.read(buffer, recordFileOffset);
      buffer.rewind();
      int recordSize = EncodingHelper.decodeLittleEndianVarInt(buffer);
      int bytesInRecordSize = buffer.position();
      if (buffer.remaining() < recordSize) {
        int newSize = recordSize + EncodingHelper.MAX_VARINT_SIZE;
        result.requiresBufferSize(newSize);
        recordFile.read(buffer, recordFileOffset + bytesInRecordSize);
        buffer.position(0);
      }
      buffer.limit(recordSize + buffer.position());
    }
  }
Esempio n. 20
0
  private void upgradeRatings(BinaryFormat newFormat) throws IOException {
    Preconditions.checkArgument(
        newFormat.getRatingSize() > format.getRatingSize(), "new format is not wider than old");
    logger.info("upgrading {} ratings from {} to {}", index, format, newFormat);

    ByteBuffer oldBuffer = ByteBuffer.allocateDirect(format.getRatingSize());
    ByteBuffer newBuffer = ByteBuffer.allocateDirect(newFormat.getRatingSize());
    MutableRating scratch = new MutableRating();

    long oldPos = BinaryHeader.HEADER_SIZE + index * format.getRatingSize();
    Preconditions.checkState(channel.position() == oldPos, "channel is at the wrong position");
    long newPos = BinaryHeader.HEADER_SIZE + index * newFormat.getRatingSize();
    channel.position(newPos);
    // loop backwards, coping each rating to later in the file
    for (int i = index - 1; i >= 0; i--) {
      oldPos -= format.getRatingSize();
      newPos -= newFormat.getRatingSize();

      // read the old rating
      BinaryUtils.readBuffer(channel, oldBuffer, oldPos);
      oldBuffer.flip();
      format.readRating(oldBuffer, scratch);
      oldBuffer.clear();

      // write the new rating
      newFormat.renderRating(scratch, newBuffer);
      newBuffer.flip();
      BinaryUtils.writeBuffer(channel, newBuffer, newPos);
      newBuffer.clear();
    }
    assert oldPos == BinaryHeader.HEADER_SIZE;
    assert newPos == BinaryHeader.HEADER_SIZE;
    format = newFormat;
    ratingBuffer = ByteBuffer.allocateDirect(newFormat.getRatingSize());
  }
  public void writeRequest(final OutputStream out) throws IOException {
    int readCount = 0;
    Iterator<OnDatatransferProgressListener> it = null;

    try {
      mChannel.position(mOffset);
      long size = mFile.length();
      if (size == 0) size = -1;
      long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
      while (mChannel.position() < maxCount) {
        readCount = mChannel.read(mBuffer);
        out.write(mBuffer.array(), 0, readCount);
        mBuffer.clear();
        if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
          mTransferred += readCount;
        }
        synchronized (mDataTransferListeners) {
          it = mDataTransferListeners.iterator();
          while (it.hasNext()) {
            it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
          }
        }
      }

    } catch (IOException io) {
      Log.e(TAG, io.getMessage());
      throw new RuntimeException(
          "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really",
          io);
    }
  }
Esempio n. 22
0
  public static void copyFile(@NotNull File from, @NotNull File to, boolean append)
      throws IOException {
    FileInputStream in = null;
    FileOutputStream out = null;

    try {
      out = createOutputStream(to, append);

      in = new FileInputStream(from);

      FileChannel readChannel = in.getChannel();
      FileChannel writeChannel = out.getChannel();

      long size = readChannel.size();

      for (long position = 0; position < size; ) {
        position += readChannel.transferTo(position, MB, writeChannel);
      }

      if (from.length() != to.length()) {
        throw new IOException("Failed to copy full contents from " + from + " to " + to);
      }
    } finally {
      close(in);
      close(out);
    }
  }
Esempio n. 23
0
 private void insertRecord(int recordPosition, long value) throws IOException {
   try {
     FileChannel channel = getFileChannel();
     long previousPosition = channel.position();
     channel.position(RECORD_SIZE * recordPosition);
     int trail = (int) (channel.size() - channel.position());
     ByteBuffer trailBuffer = null;
     if (trail > 0) {
       trailBuffer = ByteBuffer.allocate(trail);
       channel.read(trailBuffer);
       trailBuffer.flip();
     }
     ByteBuffer buffer = ByteBuffer.allocate(RECORD_SIZE);
     buffer.put(Record.IN_USE.byteValue());
     buffer.putLong(value);
     buffer.flip();
     channel.position(RECORD_SIZE * recordPosition);
     channel.write(buffer);
     if (trail > 0) {
       channel.write(trailBuffer);
     }
     channel.position(previousPosition);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Esempio n. 24
0
  /**
   * @param startByte
   * @param endByte
   * @return
   * @throws Exception
   * @return true if all the bytes between in the file between startByte and endByte are null, false
   *     otherwise
   */
  private boolean isFilePortionNull(int startByte, int endByte) throws IOException {
    logger.config("Checking file portion:" + Hex.asHex(startByte) + ":" + Hex.asHex(endByte));
    FileInputStream fis = null;
    FileChannel fc = null;
    try {
      fis = new FileInputStream(file);
      fc = fis.getChannel();
      fc.position(startByte);
      ByteBuffer bb = ByteBuffer.allocateDirect(endByte - startByte);
      fc.read(bb);
      while (bb.hasRemaining()) {
        if (bb.get() != 0) {
          return false;
        }
      }
    } finally {
      if (fc != null) {
        fc.close();
      }

      if (fis != null) {
        fis.close();
      }
    }
    return true;
  }
Esempio n. 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);
     }
   }
 }
Esempio n. 26
0
  @Override
  public boolean renameTo(File dest) throws IOException {
    if (dest == null) {
      throw new NullPointerException("dest");
    }
    if (byteBuf == null) {
      // empty file
      dest.createNewFile();
      isRenamed = true;
      return true;
    }
    int length = byteBuf.readableBytes();
    FileOutputStream outputStream = new FileOutputStream(dest);
    FileChannel fileChannel = outputStream.getChannel();
    int written = 0;
    if (byteBuf.nioBufferCount() == 1) {
      ByteBuffer byteBuffer = byteBuf.nioBuffer();
      while (written < length) {
        written += fileChannel.write(byteBuffer);
      }
    } else {
      ByteBuffer[] byteBuffers = byteBuf.nioBuffers();
      while (written < length) {
        written += fileChannel.write(byteBuffers);
      }
    }

    fileChannel.force(false);
    fileChannel.close();
    outputStream.close();
    isRenamed = true;
    return written == length;
  }
Esempio n. 27
0
 public void run(String[] args) {
   String script = null;
   try {
     FileInputStream stream = new FileInputStream(new File(args[0]));
     try {
       FileChannel fc = stream.getChannel();
       MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
       script = Charset.availableCharsets().get("UTF-8").decode(bb).toString();
     } finally {
       stream.close();
     }
   } catch (Exception ex) {
     ex.printStackTrace();
     System.exit(1);
   }
   Context cx = Context.enter();
   try {
     ScriptableObject scope = cx.initStandardObjects();
     scope.putConst("language", scope, "java");
     scope.putConst("platform", scope, "android");
     scope.put("util", scope, new Util(cx, scope));
     cx.evaluateString(scope, script, args[0], 1, null);
   } catch (Error ex) {
     ex.printStackTrace();
     System.exit(1);
   } catch (Exception ex) {
     ex.printStackTrace();
     System.exit(1);
   } finally {
     Context.exit();
   }
   System.exit(0);
 }
Esempio n. 28
0
  public String getFileBinaryBase64(String fileName) throws APIException {
    if ((new File(fileName)).exists()) {
      FileInputStream stream = null;
      try {
        stream = new FileInputStream(new File(fileName));
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        byte[] b = new byte[bb.remaining()];
        bb.get(b);

        return Base64.encodeBytes(b);
      } catch (Exception e) {
        throw new APIException(fileName + " could not have its files extracte!");
      } finally {
        try {
          stream.close();
        } catch (Exception e) {
          throw new APIException(fileName + " could not be closed!");
        }
      }
    } else {
      throw new APIException(fileName + " doesn't exist!");
    }
  }
  public String getJsonFile() {

    String jString = null;

    try {

      File dir = Environment.getExternalStorageDirectory();
      File yourFile = new File(dir, "form.json");
      FileInputStream stream = new FileInputStream(yourFile);

      try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        jString = Charset.defaultCharset().decode(bb).toString();
      } finally {
        stream.close();
      }

      jObject = new JSONObject(jString);

    } catch (Exception e) {
      e.printStackTrace();
    }

    return jObject.toString();
  }
Esempio n. 30
0
 public void put(String key, String value) throws IOException {
   int hash = hash(key);
   int i = indexFor(hash, capacity);
   int position = i * block_size;
   WordBlock b = readWordBlock(position);
   for (; b.nextPosition > 0; b = readWordBlock(b.nextPosition)) {
     Object k;
     if (hash(b.word) == hash && ((k = b.word) == key || key.equals(k))) {
       b.translate = value;
       writeWordBlock(b);
       return;
     }
   }
   if (b.nextPosition == 0) {
     b = new WordBlock(b.position);
   } else {
     int nextPosition = 0;
     if (fileChannel.size() > capacity * block_size) {
       nextPosition = (int) fileChannel.size();
     } else {
       nextPosition = capacity * block_size;
     }
     System.out.println(key + "  " + b.word);
     b.nextPosition = nextPosition;
     writeWordBlock(b);
     b = new WordBlock(nextPosition);
   }
   b.word = key;
   b.translate = value;
   b.nextPosition = -1;
   writeWordBlock(b);
 }