Example #1
0
 /**
  * Reads this struct from the specified input stream (convenience method when using Stream I/O).
  * For better performance, use of Block I/O (e.g. <code>sage.nio.channels.*</code>) is
  * recommended.
  *
  * @param in the input stream being read from.
  * @return the number of bytes read (typically the {@link #size() size} of this struct.
  * @throws IOException if an I/O error occurs.
  */
 public int read(java.io.InputStream in) throws java.io.IOException {
   ByteBuffer buffer = getByteBuffer();
   if (buffer.hasArray()) {
     int offset = buffer.arrayOffset() + getByteBufferPosition();
     return in.read(buffer.array(), offset, size());
   } else {
     synchronized (buffer) {
       if (_bytes == null) {
         _bytes = new byte[size()];
       }
       int bytesRead = in.read(_bytes);
       buffer.position(getByteBufferPosition());
       buffer.put(_bytes);
       return bytesRead;
     }
   }
 }
  protected static PlaceNameChunk readTileData(Tile tile, java.net.URL url) {
    java.io.InputStream is = null;

    try {
      String path = url.getFile();
      path =
          path.replaceAll(
              "%20", " "); // TODO: find a better way to get a path usable by FileInputStream

      java.io.FileInputStream fis = new java.io.FileInputStream(path);
      java.io.BufferedInputStream buf = new java.io.BufferedInputStream(fis);
      is = new java.util.zip.GZIPInputStream(buf);

      GMLPlaceNameSAXHandler handler = new GMLPlaceNameSAXHandler();
      javax.xml.parsers.SAXParserFactory.newInstance().newSAXParser().parse(is, handler);
      return handler.createPlaceNameChunk(tile.getPlaceNameService());
    } catch (Exception e) {
      // todo log actual error
      Logging.logger()
          .log(
              Level.FINE,
              Logging.getMessage(
                  "layers.PlaceNameLayer.ExceptionAttemptingToReadFile", url.toString()),
              e);
    } finally {
      try {
        if (is != null) is.close();
      } catch (java.io.IOException e) {
        Logging.logger()
            .log(
                Level.FINE,
                Logging.getMessage(
                    "layers.PlaceNameLayer.ExceptionAttemptingToReadFile", url.toString()),
                e);
      }
    }

    return null;
  }