/**
  * Constructs a new FileOutputStream on the File {@code file}. The parameter {@code append}
  * determines whether or not the file is opened and appended to or just opened and overwritten.
  *
  * @param file the file to which this stream writes.
  * @param append indicates whether or not to append to an existing file.
  * @throws FileNotFoundException if the {@code file} cannot be opened for writing.
  * @throws SecurityException if a {@code SecurityManager} is installed and it denies the write
  *     request.
  * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
  * @see java.lang.SecurityManager#checkWrite(String)
  */
 public FileOutputStream(File file, boolean append) throws FileNotFoundException {
   super();
   if (file.getPath().isEmpty() || file.isDirectory()) {
     throw new FileNotFoundException(file.getAbsolutePath());
   }
   fd = new FileDescriptor();
   fd.readOnly = false;
   fd.descriptor = open(file.getAbsolutePath(), append);
 }
 @TestOnly
 public String[] getVisibleFiles() {
   final ComboBoxModel model = myFileChooser.getModel();
   String[] result = new String[model.getSize()];
   for (int i = 0; i < model.getSize(); i++) {
     FileDescriptor o = (FileDescriptor) model.getElementAt(i);
     result[i] = o.getPresentableName(o.myFile.getVirtualFile());
   }
   return result;
 }
 private Descriptor getDescriptor(String descriptorFileInClassPath) {
   try {
     InputStream fin =
         this.getClass().getClassLoader().getResourceAsStream(descriptorFileInClassPath);
     FileDescriptorSet set = FileDescriptorSet.parseFrom(fin);
     FileDescriptor file = FileDescriptor.buildFrom(set.getFile(0), new FileDescriptor[] {});
     return file.getMessageTypes().get(0);
   } catch (Exception e) {
     throw Throwables.propagate(e);
   }
 }
  @Override
  protected ChannelBuffer read(int fd, long position, int length)
      throws IOException, XrootdException {
    if (fd < 0 || fd >= descriptors.size() || descriptors.get(fd) == null) {
      throw new XrootdException(kXR_FileNotOpen, "Invalid file descriptor");
    }

    FileDescriptor descriptor = descriptors.get(fd);
    byte[] chunkArray = new byte[length];
    ByteBuffer chunk = ByteBuffer.wrap(chunkArray);
    descriptor.read(chunk, position);
    return wrappedBuffer(chunkArray, 0, chunkArray.length - chunk.remaining());
  }
 /** it's called in cycle and when base image 0 doesn't exists than there is forewer loop. */
 private FileResponse getNoImageStream(ResizeRequest request) throws IOException {
   String extension = noImage.getExtension();
   if (extension == null) {
     extension = getFileExtension(noImage.getIdFile());
     if (extension == null) {
       throw new IOException("NoImage is not defined");
     }
   }
   FileDescriptor descriptor =
       new FileDescriptor(noImage.getIdFile(), request.getCode(), extension);
   if (isExists(descriptor)) {
     File file = getFileForDescription(descriptor);
     return new FileResponse(noImage.getIdFile(), file, extension, getContentType(extension));
   } else {
     FileDescriptor mainFile = new FileDescriptor(noImage.getIdFile(), null, extension);
     if (isExists(mainFile)) {
       // try to resize original image
       OutputStream os = getFileWithSameExtensionAsMain(noImage.getIdFile(), request.getCode());
       getImageResizer().resize(getFileForId(noImage.getIdFile(), ""), os, request);
       os.close();
       if (!isExists(descriptor)) {
         // load default image
         logger.debug("file " + descriptor + " wasn't found, default will be loaded");
         throw new IOException("NoImage is not defined");
       }
       File file = getFileForDescription(descriptor);
       return new FileResponse(noImage.getIdFile(), file, extension, getContentType(extension));
     } else {
       // load default image
       logger.debug("file " + descriptor + " wasn't found, default will be loaded");
       throw new IOException("NoImage is not defined");
     }
   }
 }
 /**
  * Sets the file position pointer to a new value.
  *
  * <p>The argument is the number of bytes counted from the start of the file. The position cannot
  * be set to a value that is negative. The new position can be set beyond the current file size.
  * If set beyond the current file size, attempts to read will return end of file. Write operations
  * will succeed but they will fill the bytes between the current end of file and the new position
  * with the required number of (unspecified) byte values.
  *
  * @param newPosition the new file position, in bytes.
  * @return the receiver.
  * @throws IllegalArgumentException if the new position is negative.
  * @throws ClosedChannelException if this channel is closed.
  */
 public IOCipherFileChannel position(long newPosition) throws IOException {
   if (newPosition < 0)
     throw new IllegalArgumentException("negative file position not allowed: " + newPosition);
   checkOpen();
   fd.position = newPosition;
   return this;
 }
 /**
  * Closes this stream. This implementation closes the underlying operating system resources
  * allocated to represent this stream.
  *
  * @throws IOException if an error occurs attempting to close this stream.
  */
 @Override
 public void close() throws IOException {
   if (fd == null) {
     // if fd is null, then the underlying file is not opened, so nothing
     // to close
     return;
   }
   synchronized (this) {
     if (fd.descriptor >= 0) {
       close(fd.descriptor);
       fd.descriptor = -1;
     }
   }
 }
 @Override
 public FileResponse getImage(ImageRequest imageRequest) throws IOException {
   FileResponse out =
       getImageStream(imageRequest.getIdImage(), imageRequest.getExtension(), imageRequest);
   if (out.getIdFile() == noImage.getIdFile() && imageRequest.getExtension() != null) {
     /** Image wasn't found with extension. Let's try it again without extension. */
     ImageRequest copy = new ImageRequest(imageRequest.getIdImage());
     copy.setWidth(imageRequest.getWidth());
     copy.setHeight(imageRequest.getHeight());
     copy.setKeepAspectRatio(imageRequest.isKeepAspectRatio());
     out = getImageStream(imageRequest.getIdImage(), null, copy);
     if (out.getIdFile() != noImage.getIdFile()) {
       logger.warn(
           "image '"
               + imageRequest.getIdImage()
               + "' should have extension '"
               + out.getExtension()
               + "' instead of '"
               + imageRequest.getExtension()
               + "'");
     }
   }
   return out;
 }
 /**
  * IOCipher version of POSIX lseek, since the underlying FUSE layer does not track the position in
  * open files for us, we do it inside of this class. This class wraps a {@link FileDescriptor}, so
  * you cannot specify one as an argument.
  *
  * @param offset the new position to seek to.
  * @param whence changes the pointer repositioning behavior:
  *     <ul>
  *       <li>if {@link info.guardianproject.libcore.io.OsConstants.SEEK_SET SEEK_SET} then file
  *           pointer is set to <i>offset</i>
  *       <li>if {@link info.guardianproject.libcore.io.OsConstants.SEEK_CUR SEEK_CUR} then file
  *           pointer is set to <i>current position + offset</i>
  *       <li>if {@link info.guardianproject.libcore.io.OsConstants.SEEK_END SEEK_END} then file
  *           pointer is set to <i>file size + offset</i>
  *     </ul>
  *
  * @throws ClosedChannelException if this channel is already closed.
  * @return new position of file pointer
  */
 public long lseek(long offset, int whence) throws IOException {
   checkOpen();
   long tmpPosition = fd.position;
   if (whence == SEEK_SET) {
     tmpPosition = offset;
   } else if (whence == SEEK_CUR) {
     tmpPosition += offset;
   } else if (whence == SEEK_END) {
     tmpPosition = size() + offset;
   } else {
     throw new IllegalArgumentException("Unknown 'whence': " + whence);
   }
   if (tmpPosition < 0) throw new IOException("negative resulting position: " + tmpPosition);
   else fd.position = tmpPosition;
   return fd.position;
 }
 public boolean matches(FileDescriptor fd) {
   return matches(fd.getSubPath());
 }
  protected void read(Map<Integer, ByteBuffer> tags) {
    super.read(tags);

    for (Iterator<Entry<Integer, ByteBuffer>> it = tags.entrySet().iterator(); it.hasNext(); ) {
      Entry<Integer, ByteBuffer> entry = it.next();

      ByteBuffer _bb = entry.getValue();

      switch (entry.getKey()) {
        case 0x3215:
          signalStandard = _bb.get();
          break;
        case 0x320c:
          frameLayout = LayoutType.values()[_bb.get()];
          break;
        case 0x3203:
          storedWidth = _bb.getInt();
          break;
        case 0x3202:
          storedHeight = _bb.getInt();
          break;
        case 0x3216:
          storedF2Offset = _bb.getInt();
          break;
        case 0x3205:
          sampledWidth = _bb.getInt();
          break;
        case 0x3204:
          sampledHeight = _bb.getInt();
          break;
        case 0x3206:
          sampledXOffset = _bb.getInt();
          break;
        case 0x3207:
          sampledYOffset = _bb.getInt();
          break;
        case 0x3208:
          displayHeight = _bb.getInt();
          break;
        case 0x3209:
          displayWidth = _bb.getInt();
          break;
        case 0x320a:
          displayXOffset = _bb.getInt();
          break;
        case 0x320b:
          displayYOffset = _bb.getInt();
          break;
        case 0x3217:
          displayF2Offset = _bb.getInt();
          break;
        case 0x320e:
          aspectRatio = new Rational(_bb.getInt(), _bb.getInt());
          break;
        case 0x3218:
          activeFormatDescriptor = _bb.get();
          break;
        case 0x320d:
          videoLineMap = readInt32Batch(_bb);
          break;
        case 0x320f:
          alphaTransparency = _bb.get();
          break;
        case 0x3210:
          transferCharacteristic = UL.read(_bb);
          break;
        case 0x3211:
          imageAlignmentOffset = _bb.getInt();
          break;
        case 0x3213:
          imageStartOffset = _bb.getInt();
          break;
        case 0x3214:
          imageEndOffset = _bb.getInt();
          break;
        case 0x3212:
          fieldDominance = _bb.get();
          break;
        case 0x3201:
          pictureEssenceCoding = UL.read(_bb);
          break;
        case 0x321a:
          codingEquations = UL.read(_bb);
          break;
        case 0x3219:
          colorPrimaries = UL.read(_bb);
          break;
        default:
          Logger.warn(String.format("Unknown tag [ " + ul + "]: %04x", entry.getKey()));
          continue;
      }
      it.remove();
    }
  }