/**
  * Reads all available data from the input stream of <code>conn</code> and returns it as byte
  * array. If no input data is available the method returns <code>null</code>.
  *
  * @param conn
  * @return
  * @throws IOException
  */
 protected static byte[] loadBodyDataInBuffer(HttpURLConnection conn) throws IOException {
   InputStream input = conn.getInputStream();
   byte[] data = null;
   try {
     if (Thread.currentThread() instanceof MapSourceListener) {
       // We only throttle atlas downloads, not downloads for the preview map
       long bandwidthLimit = Settings.getInstance().getBandwidthLimit();
       if (bandwidthLimit > 0) {
         input = new ThrottledInputStream(input);
       }
     }
     data = Utilities.getInputBytes(input);
   } catch (IOException e) {
     InputStream errorIn = conn.getErrorStream();
     try {
       byte[] errData = Utilities.getInputBytes(errorIn);
       log.trace(
           "Retrieved " + errData.length + " error bytes for a HTTP " + conn.getResponseCode());
     } catch (Exception ee) {
       log.debug("Error retrieving error stream content: " + e);
     } finally {
       Utilities.closeStream(errorIn);
     }
     throw e;
   } finally {
     Utilities.closeStream(input);
   }
   log.trace("Retrieved " + data.length + " bytes for a HTTP " + conn.getResponseCode());
   if (data.length == 0) return null;
   return data;
 }
  public static void load() {
    File mapSourcesDir = Settings.getInstance().getMapSourcesDirectory();
    File mapSourcesProperties = new File(mapSourcesDir, FILENAME);
    if (!mapSourcesProperties.isFile()) return;
    FileInputStream in = null;
    try {
      in = new FileInputStream(mapSourcesProperties);
      PROPERTIES.load(in);
    } catch (IOException e) {
      log.error("Failed to load mapsources.properties", e);
    } finally {
      Utilities.closeStream(in);
    }
    if (!SHUTDOWN_HOOK_REGISTERED) {
      Runtime.getRuntime()
          .addShutdownHook(
              new Thread() {

                @Override
                public void run() {
                  save();
                }
              });
    }
  }
 public static void save() {
   if (PROPERTIES.size() == 0) return;
   File mapSourcesDir = Settings.getInstance().getMapSourcesDirectory();
   File mapSourcesProperties = new File(mapSourcesDir, FILENAME);
   FileOutputStream out = null;
   try {
     out = new FileOutputStream(mapSourcesProperties);
     PROPERTIES.store(out, "");
   } catch (IOException e) {
     log.error("", e);
   } finally {
     Utilities.closeStream(out);
   }
 }