Exemplo n.º 1
0
  public byte[] getImageData(String pathInfo) throws IOException, FormatException {
    byte[] imageData = null;

    // We're assuming that all queries are coming in with the name in front,
    // along with a slash
    if (name != null) {
      int index = pathInfo.indexOf(name, 1);
      pathInfo = pathInfo.substring(index + name.length());
    }

    String filePath = rootDir + pathInfo;
    if (logger.isLoggable(Level.FINE)) {
      logger.fine("looking for " + filePath);
    }

    try {

      BinaryBufferedFile file = new BinaryBufferedFile(filePath);
      imageData = file.readBytes(100000, true);
      file.close();

    } catch (IOException ioe) {
      logger.fine("Problem fetching the file");
      // The file wasn't found.
      if (emptyTileHandler != null) {
        if (logger.isLoggable(Level.FINE)) {
          logger.fine("Creating " + filePath + " since it wasn't found from the server.");
        }

        TileInfo ti = new TileInfo(filePath); // FPBT: used to be
        // pathInfo
        ti.setMtcTransform(getMtcTransform());
        BufferedImage bufferedImage = ti.getBufferedImage(emptyTileHandler);

        if (bufferedImage != null) {
          imageData = new PNGImageIOFormatter().formatImage(bufferedImage);
          logger.fine("buffered image created, writing file to disk too");
          File newFile = new File(filePath);
          newFile.getParentFile().mkdirs();
          // Write the image data to the local cache location
          FileOutputStream fos = new FileOutputStream(newFile);
          fos.write(imageData);
          fos.flush();
          fos.close();
        } else {
          logger.fine("null buffered image back from EmptyTileHandler");
        }
      } else {
        logger.fine("no empty file handler");
      }
    }

    return imageData;
  }