<commit_before>package org.lodder.subtools.sublibrary.cache;

import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.util.Base64;
import org.lodder.subtools.sublibrary.xml.XMLHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA. User: lodder Date: 17/04/11 Time: 9:06 To change this template use File
 * | Settings | File Templates.
 */
public class DiskCacheManager extends CacheManager {

  private static DiskCacheManager dc = null;
  private HashMap<String, File> indexList = null;
  private final File path;

  private DiskCacheManager() {
    super();
    path = new File(System.getProperty("user.home"), ".MultiSubDownloader");
    if (!path.exists()) {
      if (!path.mkdir()) {
        throw new RuntimeException("Could not create folder " + path);
      }
    }
    indexList = new HashMap<String, File>();
    if (new File(path, "index").exists()) loadIndex();
  }

  public static DiskCacheManager getDiskCache() {
    if (dc == null) dc = new DiskCacheManager();
    return dc;
  }

  private void loadIndex() {
    Document newDoc;
    try {
      newDoc =
          DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path, "index"));
      NodeList nList = newDoc.getElementsByTagName("CacheEntry");

      for (int i = 0; i < nList.getLength(); i++) {
        if (nList.item(i).getNodeType() == Node.ELEMENT_NODE) {
          boolean enc =
              XMLHelper.getBooleanAtributeValue("cacheurl", "encode", (Element) nList.item(i));

          String key = "";
          if (enc) {
            String raw = XMLHelper.getStringTagRawValue("cacheurl", (Element) nList.item(i));
            key = new String(Base64.decode(raw), "UTF-8");
          } else {
            key = XMLHelper.getStringTagValue("cacheurl", (Element) nList.item(i));
          }
          String value = XMLHelper.getStringTagValue("cachefile", (Element) nList.item(i));
          indexList.put(key, new File(value));
        }
      }
    } catch (ParserConfigurationException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (SAXException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (IOException e) {
      Logger.instance.error(Logger.stack2String(e));
    }

  }

  private void storeIndex() {
    Document newDoc;
    try {
      newDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
      Element rootElement = newDoc.createElement("DiskCacheManager");
      newDoc.appendChild(rootElement);

      for (String key : indexList.keySet()) {
        String value = indexList.get(key).toString();
        Element cacheEntry = newDoc.createElement("CacheEntry");


        String encKey = new String(Base64.encode(key.getBytes()));
        Element cacheUrl = newDoc.createElement("cacheurl");
        cacheUrl.setAttribute("encode", "true");
        cacheUrl.appendChild(newDoc.createTextNode(encKey));

        cacheEntry.appendChild(cacheUrl);

        Element cacheFile = newDoc.createElement("cachefile");
        cacheFile.appendChild(newDoc.createTextNode(value));<commit_msg>Fix . defect: Found reliance on default encoding: String.getBytes().<commit_after>package org.lodder.subtools.sublibrary.cache;

import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.util.Base64;
import org.lodder.subtools.sublibrary.xml.XMLHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA. User: lodder Date: 17/04/11 Time: 9:06 To change this template use File
 * | Settings | File Templates.
 */
public class DiskCacheManager extends CacheManager {

  private static DiskCacheManager dc = null;
  private HashMap<String, File> indexList = null;
  private final File path;

  private DiskCacheManager() {
    super();
    path = new File(System.getProperty("user.home"), ".MultiSubDownloader");
    if (!path.exists()) {
      if (!path.mkdir()) {
        throw new RuntimeException("Could not create folder " + path);
      }
    }
    indexList = new HashMap<String, File>();
    if (new File(path, "index").exists()) loadIndex();
  }

  public static DiskCacheManager getDiskCache() {
    if (dc == null) dc = new DiskCacheManager();
    return dc;
  }

  private void loadIndex() {
    Document newDoc;
    try {
      newDoc =
          DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path, "index"));
      NodeList nList = newDoc.getElementsByTagName("CacheEntry");

      for (int i = 0; i < nList.getLength(); i++) {
        if (nList.item(i).getNodeType() == Node.ELEMENT_NODE) {
          boolean enc =
              XMLHelper.getBooleanAtributeValue("cacheurl", "encode", (Element) nList.item(i));

          String key = "";
          if (enc) {
            String raw = XMLHelper.getStringTagRawValue("cacheurl", (Element) nList.item(i));
            key = new String(Base64.decode(raw), "UTF-8");
          } else {
            key = XMLHelper.getStringTagValue("cacheurl", (Element) nList.item(i));
          }
          String value = XMLHelper.getStringTagValue("cachefile", (Element) nList.item(i));
          indexList.put(key, new File(value));
        }
      }
    } catch (ParserConfigurationException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (SAXException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (IOException e) {
      Logger.instance.error(Logger.stack2String(e));
    }

  }

  private void storeIndex() {
    Document newDoc;
    try {
      newDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
      Element rootElement = newDoc.createElement("DiskCacheManager");
      newDoc.appendChild(rootElement);

      for (String key : indexList.keySet()) {
        String value = indexList.get(key).toString();
        Element cacheEntry = newDoc.createElement("CacheEntry");


        String encKey = Base64.encode(key.getBytes());
        Element cacheUrl = newDoc.createElement("cacheurl");
        cacheUrl.setAttribute("encode", "true");
        cacheUrl.appendChild(newDoc.createTextNode(encKey));

        cacheEntry.appendChild(cacheUrl);

        Element cacheFile = newDoc.createElement("cachefile");
        cacheFile.appendChild(newDoc.createTextNode(value));
{'fraction_non_alphanumeric': 0.1292223659623401, 'fraction_numerical': 0.0068995256576110395, 'mean_word_length': 3.3569192235441454, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2374394', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2023, 'n_words': 431}