<commit_before>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.p2.internal.core;

import org.eclipse.oomph.p2.core.Agent;
import org.eclipse.oomph.util.IOUtil;
import org.eclipse.oomph.util.PropertiesUtil;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.repository.AuthenticationFailedException;
import org.eclipse.equinox.internal.p2.repository.DownloadStatus;
import org.eclipse.equinox.internal.p2.repository.Transport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.List;

/**
 * @author <NAME>
 */
@SuppressWarnings("restriction")
public class CachingTransport extends Transport
{
  private static final Long NOT_FOUND = Long.MIN_VALUE;

  private static final String NOT_FOUND_TOKEN = "NOT FOUND";

  private final Agent agent;

  private final Transport delegate;

  private final File cacheFolder;

  public CachingTransport(Agent agent, Transport delegate)
  {
    this.agent = agent;
    this.delegate = delegate;

    File folder = P2CorePlugin.getUserStateFolder(new File(PropertiesUtil.USER_HOME));
    cacheFolder = new File(folder, "cache");
    cacheFolder.mkdirs();

    // TODO Delete this legacy code for 1.0 release
    File oldCacheFile = new File(folder, "metadata.cache");
    if (oldCacheFile.exists())
    {
      IOUtil.deleteBestEffort(oldCacheFile);
    }
  }

  public boolean isOffline()
  {
    return agent.isOffline();
  }

  @Override
  public IStatus download(URI uri, OutputStream target, long startPos, IProgressMonitor monitor)
  {
    File modifiedFile = getModifiedFile(uri);
    File contentFile = getContentFile(uri);

    if (isOffline())
    {
      Long modified = loadModified(modifiedFile);
      if (NOT_FOUND.equals(modified))
      {
        return P2CorePlugin.INSTANCE.getStatus(new Exception());
      }

      byte[] b = loadContent(contentFile);
      if (b != null)
      {
        IOUtil.copy(new ByteArrayInputStream(b), target);
        return Status.OK_STATUS;
      }
    }

    OutputStream oldTarget = null;
    if (uri.getPath().endsWith("/p2.index"))
    {
      oldTarget = target;
      target = new ByteArrayOutputStream();
    }

    IStatus status = delegate.download(uri, target, startPos, monitor);
    if (!status.isOK())
    {
      saveModified(modifiedFile, NOT_FOUND);
    }
    else
    {
      long lastModified = NOT_FOUND;
      if (status instanceof DownloadStatus)
      {
        DownloadStatus downloadStatus = (DownloadStatus)status;
        lastModified = downloadStatus.getLastModified();
        saveModified(modifiedFile, lastModified);
      }

      if (oldTarget != null)
      {
        byte[] content = ((ByteArrayOutputStream)target).toByteArray();
        IOUtil.copy(new ByteArrayInputStream(content), oldTarget);
        saveContent(contentFile, content, lastModified);
      }
    }

    return status;
  }<commit_msg>[437818] Error in CachingTransport  https://bugs.eclipse.org/bugs/show_bug.cgi?id=437818<commit_after>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.p2.internal.core;

import org.eclipse.oomph.p2.core.Agent;
import org.eclipse.oomph.util.IOUtil;
import org.eclipse.oomph.util.PropertiesUtil;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.repository.AuthenticationFailedException;
import org.eclipse.equinox.internal.p2.repository.DownloadStatus;
import org.eclipse.equinox.internal.p2.repository.Transport;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.List;

/**
 * @author <NAME>
 */
@SuppressWarnings("restriction")
public class CachingTransport extends Transport
{
  private static final Long NOT_FOUND = Long.MIN_VALUE;

  private static final String NOT_FOUND_TOKEN = "NOT FOUND";

  private final Agent agent;

  private final Transport delegate;

  private final File cacheFolder;

  public CachingTransport(Agent agent, Transport delegate)
  {
    this.agent = agent;
    this.delegate = delegate;

    File folder = P2CorePlugin.getUserStateFolder(new File(PropertiesUtil.USER_HOME));
    cacheFolder = new File(folder, "cache");
    cacheFolder.mkdirs();

    // TODO Delete this legacy code for 1.0 release
    File oldCacheFile = new File(folder, "metadata.cache");
    if (oldCacheFile.exists())
    {
      IOUtil.deleteBestEffort(oldCacheFile);
    }
  }

  public boolean isOffline()
  {
    return agent.isOffline();
  }

  @Override
  public IStatus download(URI uri, OutputStream target, long startPos, IProgressMonitor monitor)
  {
    File modifiedFile = getModifiedFile(uri);
    File contentFile = getContentFile(uri);

    if (isOffline())
    {
      Long modified = loadModified(modifiedFile);
      if (NOT_FOUND.equals(modified))
      {
        return P2CorePlugin.INSTANCE.getStatus(new Exception());
      }

      byte[] b = loadContent(contentFile);
      if (b != null)
      {
        IOUtil.copy(new ByteArrayInputStream(b), target);
        return Status.OK_STATUS;
      }
    }

    OutputStream oldTarget = null;
    String path = uri.getPath();
    if (path != null && path.endsWith("/p2.index"))
    {
      oldTarget = target;
      target = new ByteArrayOutputStream();
    }

    IStatus status = delegate.download(uri, target, startPos, monitor);
    if (!status.isOK())
    {
      saveModified(modifiedFile, NOT_FOUND);
    }
    else
    {
      long lastModified = NOT_FOUND;
      if (status instanceof DownloadStatus)
      {
        DownloadStatus downloadStatus = (DownloadStatus)status;
        lastModified = downloadStatus.getLastModified();
        saveModified(modifiedFile, lastModified);
      }

      if (oldTarget != null)
      {
        byte[] content = ((ByteArrayOutputStream)target).toByteArray();
        IOUtil.copy(new ByteArrayInputStream(content), oldTarget);
        saveContent(contentFile, content, lastModified);
      }
    }

    return status;
  }
{'fraction_non_alphanumeric': 0.10433812247175338, 'fraction_numerical': 0.006695494490165993, 'mean_word_length': 3.563971992361553, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2566923', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2064, 'n_words': 493}