示例#1
0
  /**
   * Writes a ZIP {@link File} containing a list of {@link File}.
   *
   * @param file for writing
   * @param files for the ZIP file
   * @throws IOException
   * @see File
   * @since 0.0.1
   */
  public static void writeZip(final File file, final File... files) throws IOException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(file, files));

    writeZip(file, files, Constants.DEFAULT_FILE_BUFFER_SIZE);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }
示例#2
0
  /**
   * Writes a ZIP {@link File} containing a list of {@link File}.
   *
   * @param file for writing
   * @param files for the ZIP file
   * @param bufferSize in bytes
   * @throws IOException
   * @see File
   * @since 0.0.1
   */
  public static void writeZip(final File file, final File[] files, final int bufferSize)
      throws IOException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(file, files, bufferSize));
    if (null == file) {
      throw new RuntimeExceptionIsNull("file"); // $NON-NLS-1$
    }
    if (null == files) {
      throw new RuntimeExceptionIsNull("files"); // $NON-NLS-1$
    }
    if (!HelperArray.isValid(files)) {
      throw new RuntimeExceptionIsEmpty("files"); // $NON-NLS-1$
    }
    if (1 > bufferSize) {
      throw new RuntimeExceptionMustBeGreater("bufferSize", bufferSize, 1); // $NON-NLS-1$
    }
    // //check all list entries
    // for (final File entry : files) {
    // if (null == entry) {
    //				throw new RuntimeExceptionIsNull("entry"); //$NON-NLS-1$
    // }
    // if (!entry.exists()) {
    //				throw new RuntimeExceptionFileNotExist("entry", entry); //$NON-NLS-1$
    // }
    // }

    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
      // create a ZipOutputStream to zip the data to

      for (final File entry : files) {
        addEntry(zos, entry, bufferSize);
      }
    }
    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }
示例#3
0
  private static void addEntry(final ZipOutputStream zos, final File file, final int bufferSize)
      throws IOException {
    if (log.isTraceEnabled()) log.trace(HelperLog.methodStart(zos, file, bufferSize));
    final byte[] buffer = new byte[bufferSize];

    // create a new zip entry
    final ZipEntry entry =
        new ZipEntry(
            file.getPath() + (file.isDirectory() ? "/" : HelperString.EMPTY_STRING)); // $NON-NLS-1$

    // place the zip entry in the ZipOutputStream object
    zos.putNextEntry(entry);

    if (!file.isDirectory()) {
      try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
        int offset;

        // now write the content of the file to the ZipOutputStream
        while (-1 != (offset = bis.read(buffer))) {
          zos.write(buffer, 0, offset);
        }
      }
    }
    if (log.isTraceEnabled()) log.trace(HelperLog.methodExit());
  }
示例#4
0
  /**
   * Extracts a ZIP {@link File} to a destination directory.
   *
   * @param file to extract
   * @param destinationDirectory for the ZIP file
   * @param bufferSize in bytes
   * @throws IOException
   * @see File
   * @since 0.0.1
   */
  public static void extractZip(
      final File file, final File destinationDirectory, final int bufferSize)
      throws IOException { // $JUnit$
    if (log.isDebugEnabled())
      log.debug(HelperLog.methodStart(file, destinationDirectory, bufferSize));
    if (null == file) {
      throw new RuntimeExceptionIsNull("file"); // $NON-NLS-1$
    }
    if (null == destinationDirectory) {
      throw new RuntimeExceptionIsNull("destinationDirectory"); // $NON-NLS-1$
    }
    if (1 > bufferSize) {
      throw new RuntimeExceptionMustBeGreater("bufferSize", bufferSize, 1); // $NON-NLS-1$
    }

    try (ZipFile zf = new ZipFile(file)) {
      final Enumeration<? extends ZipEntry> zipEntryEnum = zf.entries();

      while (zipEntryEnum.hasMoreElements()) {
        final ZipEntry zipEntry = zipEntryEnum.nextElement();
        extractEntry(zf, zipEntry, destinationDirectory, bufferSize);
      }
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }
示例#5
0
  /**
   * Extracts a ZIP {@link File} to a destination directory.
   *
   * @param file to extract
   * @param destinationDirectory for the ZIP file
   * @throws IOException
   * @see File
   * @since 0.0.1
   */
  public static void extractZip(final File file, final File destinationDirectory)
      throws IOException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(file, destinationDirectory));

    extractZip(file, destinationDirectory, Constants.DEFAULT_FILE_BUFFER_SIZE);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }
示例#6
0
  private static <T> Unmarshaller getUnmarshaller(final Class<T> clazz) throws JAXBException {
    if (log.isTraceEnabled()) log.trace(HelperLog.methodStart(clazz));

    final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    final Unmarshaller result = jaxbContext.createUnmarshaller();

    if (log.isTraceEnabled()) log.trace(HelperLog.methodExit(result));
    return result;
  }
示例#7
0
  private static <T> Marshaller getMarshaller(final Class<T> clazz) throws JAXBException {
    if (log.isTraceEnabled()) log.trace(HelperLog.methodStart(clazz));
    final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    final Marshaller result = jaxbContext.createMarshaller();
    //		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    result.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    if (log.isTraceEnabled()) log.trace(HelperLog.methodExit(result));
    return result;
  }
示例#8
0
  /**
   * Serialize data to XML and store it into a {@link File}.
   *
   * @param file to store the serialized data
   * @param data (object) to serialize as XML
   * @see File
   * @throws JAXBException
   * @since 0.9.0
   */
  public static <T> void serialize(final File file, final T data) throws JAXBException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(file, data));
    if (null == file) {
      throw new RuntimeExceptionIsNull("file"); // $NON-NLS-1$
    }
    if (null == data) {
      throw new RuntimeExceptionIsNull("data"); // $NON-NLS-1$
    }

    getMarshaller(data.getClass()).marshal(data, file);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }
示例#9
0
  /**
   * Returns the current screen color model as a {@link ColorModel}.
   *
   * @return current screen color model as a {@link ColorModel}
   * @see ColorModel
   * @since 0.0.1
   */
  public static ColorModel getCurrentColorModel() { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart());

    ColorModel result = null;

    try {
      result = Toolkit.getDefaultToolkit().getColorModel();
    } catch (HeadlessException ex) {
      if (log.isWarnEnabled()) log.warn("Could not get the color model", ex); // $NON-NLS-1$
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#10
0
  /**
   * Returns the current screen size in pixels as a {@link Dimension}.
   *
   * @return current screen size as a {@link Dimension}
   * @see Dimension
   * @since 0.0.1
   */
  public static Dimension getCurrentScreenSize() { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart());

    Dimension result = null;

    try {
      result = Toolkit.getDefaultToolkit().getScreenSize();
    } catch (HeadlessException ex) {
      if (log.isWarnEnabled()) log.warn("Could not get the screen size", ex); // $NON-NLS-1$
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#11
0
  /**
   * Checks if the given screen size fits to the current screen size.
   *
   * @param minSize given screen size as a {@link Dimension}
   * @return true/false
   * @see Dimension
   * @since 0.0.1
   */
  public static boolean isValidScreenSize(final Dimension minSize) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(minSize));
    if (null == minSize) {
      throw new RuntimeExceptionIsNull("minSize"); // $NON-NLS-1$
    }

    final Dimension resolution = getCurrentScreenSize();
    final boolean result =
        null != getCurrentScreenSize()
            && resolution.width >= minSize.width
            && resolution.height >= minSize.height;

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#12
0
  /**
   * Serialize data to XML and return it as {@link String}.
   *
   * @param data (object) to serialize as XML
   * @see String
   * @throws JAXBException
   * @throws IOException
   * @since 0.9.4
   */
  public static <T> String serialize(final T data) throws JAXBException, IOException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(data));

    final ByteArrayOutputStream os = new ByteArrayOutputStream();

    serialize(os, data);

    final String result =
        new String(
            HelperIO.readStream(HelperIO.convertOutputToInputStream(os)),
            Constants.ENCODING_DEFAULT);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#13
0
  /**
   * Deserialize data from A XML {@link InputStream}.
   *
   * @param is {@link InputStream} containing the serialized data
   * @param clazz for the serialized data
   * @return data as object
   * @see InputStream
   * @throws JAXBException
   * @since 0.9.0
   */
  @SuppressWarnings("unchecked")
  public static <T> T deserialize(final InputStream is, final Class<T> clazz)
      throws JAXBException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(is, clazz));
    if (null == is) {
      throw new RuntimeExceptionIsNull("is"); // $NON-NLS-1$
    }
    if (null == clazz) {
      throw new RuntimeExceptionIsNull("clazz"); // $NON-NLS-1$
    }

    final T result = (T) getUnmarshaller(clazz).unmarshal(is);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#14
0
  /**
   * Deserialize data from a XML {@link String}.
   *
   * @param input {@link String} containing the serialized data
   * @param clazz for the serialized data
   * @return data as object
   * @see String
   * @throws JAXBException
   * @since 0.9.4
   */
  public static <T> T deserialize(final String input, final Class<T> clazz)
      throws JAXBException { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(input, clazz));
    if (null == input) {
      throw new RuntimeExceptionIsNull("input"); // $NON-NLS-1$
    }

    InputStream is = null;
    try {
      is = new ByteArrayInputStream(input.getBytes(Constants.ENCODING_DEFAULT));
    } catch (UnsupportedEncodingException ex) {
      // should never happen!
      log.error("Encoding invalid", ex); // $NON-NLS-1$
    }

    final T result = deserialize(is, clazz);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#15
0
  /**
   * This method ensures that the output String has only valid XML unicode characters as specified
   * by the XML 1.0 standard. For reference, please see the <a
   * href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">standard</a>.
   *
   * @param input {@link String} to remove non-valid characters
   * @return stripped {@link String}
   * @since 0.3.0
   */
  public static String getValidXmlString(final String input) { // $JUnit$
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(input));
    if (null == input) {
      throw new RuntimeExceptionIsNull("input"); // $NON-NLS-1$
    }

    final StringBuilder sb = new StringBuilder(input.length());

    for (final int current : input.toCharArray()) {
      if (127 != current
          && // delete
          144 != current
          && // Num lock
          145 != current
          && // Scroll lock
          154 != current
          && // print
          155 != current
          && // insert
          157 != current
          && // Mac cmd
          524 != current
          && // Windows cmd
          525 != current) { // Windows context menu)
        if (9 == current
            || 10 == current
            || 13 == current
            || (41 <= current && 111 >= current)
            || (124 <= current && 55295 >= current)
            || (57344 <= current && 65533 >= current)) {
          sb.append((char) current);
        }
      }
    }
    final String result = sb.toString();

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }
示例#16
0
  private static void extractEntry(
      final ZipFile zipFile, final ZipEntry entry, final File destDir, final int bufferSize)
      throws IOException {
    if (log.isTraceEnabled()) log.trace(HelperLog.methodStart(zipFile, entry, destDir, bufferSize));
    final File file = new File(destDir, entry.getName());

    if (entry.isDirectory()) {
      file.mkdirs();
    } else {
      new File(file.getParent()).mkdirs();

      final byte[] buffer = new byte[bufferSize];

      try (BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {

        int offset;
        while (-1 != (offset = bis.read(buffer))) {
          bos.write(buffer, 0, offset);
        }
      }
    }
    if (log.isTraceEnabled()) log.trace(HelperLog.methodExit());
  }