All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.crsh.vfs.Handle Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2012 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.crsh.vfs;

import org.crsh.util.Utils;
import org.crsh.vfs.spi.FSDriver;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class Handle {

  /** . */
  private final FSDriver driver;

  /** . */
  final String name;

  /** . */
  final H handle;

  Handle(FSDriver driver, H handle) throws IOException {
    String name = driver.name(handle);

    //
    this.driver = driver;
    this.handle = handle;
    this.name = name;
  }

  Iterable> children() throws IOException {
    List> children = new ArrayList>();
    for (H h : driver.children(handle)) {
      children.add(new Handle(driver, h));
    }
    return children;
  }

  Resource getResource() throws IOException {
    InputStream in = open();
    byte[] bytes = Utils.readAsBytes(in);
    long lastModified = getLastModified();
    return new Resource(name, bytes, lastModified);
  }

  Iterator getResources() throws IOException {
    Iterator i = driver.open(handle);
    if (i.hasNext()) {
      LinkedList resources = new LinkedList();
      while (i.hasNext()) {
        InputStream in = i.next();
        byte[] bytes = Utils.readAsBytes(in);
        long lastModified = getLastModified();
        resources.add(new Resource(name, bytes, lastModified));
      }
      return resources.iterator();
    } else {
      return Utils.iterator();
    }
  }

  private InputStream open() throws IOException {
    Iterator i = driver.open(handle);
    if (i.hasNext()) {
      return i.next();
    } else {
      throw new IOException("No stream");
    }
  }

  long getLastModified() throws IOException {
    return driver.getLastModified(handle);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy