Writer.java

  1. package org.opentrafficsim.base.compressedfiles;

  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.zip.GZIPOutputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;

  8. /**
  9.  * Writer for compressed files.
  10.  * <p>
  11.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  12.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  13.  * <p>
  14.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 24, 2018 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  17.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  18.  */
  19. public final class Writer
  20. {
  21.     /**
  22.      * Class with only static methods should not be instantiated.
  23.      */
  24.     private Writer()
  25.     {
  26.         // Do not instantiate
  27.     }

  28.     /**
  29.      * Construct a new OutputStream that performs a requested data compression.
  30.      * @param fileName String; name of the file that will be created (possible
  31.      * @param compressionType CompressionType; the type of data compression to use
  32.      * @return OutputStream that accepts data to write and should eventually be closed
  33.      * @throws IOException when the requested file could not be opened for writing, or the compression type is unknown
  34.      */
  35.     public static OutputStream createOutputStream(final String fileName, final CompressionType compressionType)
  36.             throws IOException
  37.     {
  38.         if (CompressionType.AUTODETECT.equals(compressionType))
  39.         {
  40.             throw new IOException("AUTODETECT not allowed for Writer");
  41.         }
  42.         String extension = "." + compressionType.getExtension();
  43.         int suffixPos = fileName.length() - extension.length();
  44.         String currentSuffix = suffixPos > 0 ? fileName.substring(suffixPos) : "";
  45.         String fixedFileName =
  46.                 (currentSuffix.equalsIgnoreCase(extension) ? fileName.substring(0, suffixPos) : fileName) + extension;
  47.         FileOutputStream fileOutputStream = new FileOutputStream(fixedFileName);
  48.         switch (compressionType)
  49.         {
  50.             case AUTODETECT:
  51.                 // Cannot happen
  52.                 throw new IOException("AUTODETECT not allowed for Writer");

  53.             case GZIP:
  54.                 return new GZIPOutputStream(fileOutputStream);

  55.             case NONE:
  56.                 return fileOutputStream;

  57.             case ZIP:
  58.             {
  59.                 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
  60.                 // Construct the name of the (single) TOC entry from the supplied file name
  61.                 int lastSeparatorPos =
  62.                         Math.max(Math.max(fileName.lastIndexOf("/"), fileName.lastIndexOf("\\")), fileName.lastIndexOf(":"));
  63.                 String entryName = fileName.substring(lastSeparatorPos + 1);
  64.                 zipOutputStream.putNextEntry(new ZipEntry(entryName));
  65.                 return zipOutputStream;
  66.             }

  67.             default:
  68.                 // Cannot happen
  69.                 throw new IOException("Don't know how to create writer for compression type " + compressionType);

  70.         }
  71.     }

  72. }