/*
 * Copyright 2012-2015 Johns Hopkins University HLTCOE. All rights reserved.
 * See LICENSE in the project root directory.
 */
package edu.jhu.hlt.acute.archivers.tar;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import edu.jhu.hlt.acute.archivers.Archivable;
import edu.jhu.hlt.acute.archivers.Archiver;

/**
 * A class that provides the ability to write entries to a .tar archive
 * in a forward-only fashion. New additions will overwrite files with the 
 * same file name.
 * <br/>
 * <br/>
 * This implementation appends a <code>.</code> for the extension if the extension does not begin
 * with a <code>.</code>. 
 * <br/>
 * <br/>
 * For example, if the {@link Archivable} implementation's 
 * {@link Archivable#getExtension()} method returns
 * <code>elf</code>, this implementation will create archives with the name <code>fileName.elf</code>.
 */
public class TarArchiver implements Archiver {

  private final TarArchiveOutputStream tos;
  
  /**
   * Wrap an {@link OutputStream}. 
   */
  public TarArchiver(OutputStream os) {
    this.tos = new TarArchiveOutputStream(os);
  }
  
  /*
   * (non-Javadoc)
   * @see edu.jhu.hlt.acute.archivers.Archiver#addEntry(edu.jhu.hlt.acute.archivers.Archivable)
   */
  @Override
  public void addEntry(Archivable arch) throws IOException {
    final String fn = arch.getFileName();
    TarArchiveEntry entry = new TarArchiveEntry(fn);
    byte[] cbytes = arch.getBytes();
    entry.setSize(cbytes.length);
    this.tos.putArchiveEntry(entry);
    try (ByteArrayInputStream bis = new ByteArrayInputStream(cbytes)) {
      IOUtils.copy(bis, tos);
      tos.closeArchiveEntry();
    }
  }

  /* (non-Javadoc)
   * @see java.lang.AutoCloseable#close()
   */
  @Override
  public void close() throws IOException {
    this.tos.close();
  }
}

@@ -692,18 +692,17 @@
 with the
- 
 %0A
+
  * same 
@@ -716,384 +716,8 @@
 me.%0A
- * %3Cbr/%3E%0A * %3Cbr/%3E%0A * This implementation appends a %3Ccode%3E.%3C/code%3E for the extension if the extension does not begin%0A * with a %3Ccode%3E.%3C/code%3E. %0A * %3Cbr/%3E%0A * %3Cbr/%3E%0A * For example, if the %7B@link Archivable%7D implementation's %0A * %7B@link Archivable#getExtension()%7D method returns%0A * %3Ccode%3Eelf%3C/code%3E, this implementation will create archives with the name %3Ccode%3EfileName.elf%3C/code%3E.%0A
  */%0A
@@ -808,19 +808,17 @@
 am tos;%0A
-  
 %0A
+
   /**%0A  
@@ -849,17 +849,16 @@
 Stream%7D.
- 
 %0A   */%0A 
@@ -947,18 +947,16 @@
 s);%0A  %7D%0A
-  
 %0A  /*%0A  

Fix Javadoc warnings.