Java Code Examples for com.helger.commons.io.file.FileHelper#getOutputStream()

The following examples show how to use com.helger.commons.io.file.FileHelper#getOutputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicroWriter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Write a Micro Node to a file.
 *
 * @param aNode
 *        The node to be serialized. May be any kind of node (incl.
 *        documents). May not be <code>null</code>.
 * @param aFile
 *        The file to write to. May not be <code>null</code>.
 * @param aSettings
 *        The settings to be used for the creation. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
public static ESuccess writeToFile (@Nonnull final IMicroNode aNode,
                                    @Nonnull final File aFile,
                                    @Nonnull final IXMLWriterSettings aSettings)
{
  ValueEnforcer.notNull (aFile, "File");

  final OutputStream aOS = FileHelper.getOutputStream (aFile);
  if (aOS == null)
    return ESuccess.FAILURE;

  // No need to wrap the OS in a BufferedOutputStream as inside, it is later
  // on wrapped in a BufferedWriter
  return writeToStream (aNode, aOS, aSettings);
}
 
Example 2
Source File: AbstractSimpleDAO.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * The main method for writing the new data to a file. This method may only be
 * called within a write lock!
 *
 * @return {@link ESuccess} and never <code>null</code>.
 */
@Nonnull
@SuppressFBWarnings ("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@MustBeLocked (ELockType.WRITE)
private ESuccess _writeToFile ()
{
  // Build the filename to write to
  final String sFilename = m_aFilenameProvider.get ();
  if (sFilename == null)
  {
    // We're not operating on a file! Required for testing
    if (!isSilentMode ())
      if (LOGGER.isInfoEnabled ())
        LOGGER.info ("The DAO of class " + getClass ().getName () + " cannot write to a file");
    return ESuccess.FAILURE;
  }

  // Check for a filename change before writing
  if (!sFilename.equals (m_sPreviousFilename))
  {
    onFilenameChange (m_sPreviousFilename, sFilename);
    m_sPreviousFilename = sFilename;
  }

  if (!isSilentMode ())
    if (LOGGER.isInfoEnabled ())
      LOGGER.info ("Trying to write DAO file '" + sFilename + "'");

  File aFile = null;
  IMicroDocument aDoc = null;
  try
  {
    // Get the file handle
    aFile = getSafeFile (sFilename, EMode.WRITE);

    m_aStatsCounterWriteTotal.increment ();
    final StopWatch aSW = StopWatch.createdStarted ();

    // Create XML document to write
    aDoc = createWriteData ();
    if (aDoc == null)
      throw new DAOException ("Failed to create data to write to file");

    // Generic modification
    modifyWriteData (aDoc);

    // Perform optional stuff like backup etc. Must be done BEFORE the output
    // stream is opened!
    beforeWriteToFile (sFilename, aFile);

    // Get the output stream
    final OutputStream aOS = FileHelper.getOutputStream (aFile);
    if (aOS == null)
    {
      // Happens, when another application has the file open!
      // Logger warning already emitted
      throw new DAOException ("Failed to open output stream");
    }

    // Write to file (closes the OS)
    final IXMLWriterSettings aXWS = getXMLWriterSettings ();
    if (MicroWriter.writeToStream (aDoc, aOS, aXWS).isFailure ())
      throw new DAOException ("Failed to write DAO XML data to file");

    m_aStatsCounterWriteTimer.addTime (aSW.stopAndGetMillis ());
    m_aStatsCounterWriteSuccess.increment ();
    m_nWriteCount++;
    m_aLastWriteDT = PDTFactory.getCurrentLocalDateTime ();
    return ESuccess.SUCCESS;
  }
  catch (final Exception ex)
  {
    final String sErrorFilename = aFile != null ? aFile.getAbsolutePath () : sFilename;

    if (LOGGER.isErrorEnabled ())
      LOGGER.error ("The DAO of class " +
                    getClass ().getName () +
                    " failed to write the DAO data to '" +
                    sErrorFilename +
                    "'",
                    ex);

    triggerExceptionHandlersWrite (ex, sErrorFilename, aDoc);
    m_aStatsCounterWriteExceptions.increment ();
    return ESuccess.FAILURE;
  }
}
 
Example 3
Source File: FileSystemResource.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public FileOutputStream getOutputStream (@Nonnull final EAppend eAppend)
{
  return FileHelper.getOutputStream (m_aFile, eAppend);
}
 
Example 4
Source File: FileSystemByteStreamProvider.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public OutputStream getOutputStream (@Nonnull final String sName, @Nonnull final EAppend eAppend)
{
  return FileHelper.getOutputStream (new File (m_aBasePath, sName), eAppend);
}
 
Example 5
Source File: ISettingsPersistence.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Write settings to a file.
 *
 * @param aSettings
 *        The settings to be written. May not be <code>null</code>.
 * @param aFile
 *        The file where the settings should be written to. May not be
 *        <code>null</code>.
 * @return Success and never <code>null</code>.
 */
@Nonnull
default ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull final File aFile)
{
  final OutputStream aOS = FileHelper.getOutputStream (aFile);
  if (aOS == null)
    throw new IllegalArgumentException ("Failed to open file '" + aFile.getAbsolutePath () + "' for writing!");
  return writeSettings (aSettings, aOS);
}