001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io; 020 021import javax.inject.Inject; 022import java.io.File; 023import java.io.IOException; 024import java.nio.file.Files; 025import java.nio.file.attribute.FileAttribute; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028 029/** 030 * A TempFileCreator that uses the system's temp directory. 031 */ 032public final class SystemTempFileCreator implements TempFileCreator { 033 private static final Logger LOG = Logger.getLogger(SystemTempFileCreator.class.getName()); 034 035 @Inject 036 public SystemTempFileCreator() { 037 LOG.log(Level.FINE, "Temporary files and folders will be created in the system temp folder."); 038 } 039 040 @Override 041 public File createTempFile(final String prefix, final String suffix) throws IOException { 042 final File result = File.createTempFile(prefix, suffix); 043 if (LOG.isLoggable(Level.FINEST)) { 044 LOG.log(Level.FINEST, "Created temporary file: {0}", result.getAbsolutePath()); 045 } 046 return result; 047 } 048 049 @Override 050 public File createTempDirectory(final String prefix, final FileAttribute<?> attributes) throws IOException { 051 final File result = Files.createTempDirectory(prefix, attributes).toFile(); 052 if (LOG.isLoggable(Level.FINEST)) { 053 LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath()); 054 } 055 return result; 056 } 057 058 @Override 059 public File createTempDirectory(final String prefix) throws IOException { 060 final File result = Files.createTempDirectory(prefix).toFile(); 061 if (LOG.isLoggable(Level.FINEST)) { 062 LOG.log(Level.FINEST, "Created temporary folder: {0}", result.getAbsolutePath()); 063 } 064 return result; 065 } 066}