1 52 53 package com.go.trove.log; 54 55 import java.io.*; 56 import java.util.*; 57 import java.text.*; 58 59 66 public class FileLogStreamFactory implements IntervalLogStream.Factory { 67 private File mDirectory; 68 private DateFormat mDateFormat; 69 private String mExtension; 70 71 84 public FileLogStreamFactory(File directory, 85 DateFormat format, 86 String extension) { 87 88 if (directory == null) { 89 throw new NullPointerException 90 ("FileLogStreamFactory directory not specified"); 91 } 92 93 try { 94 mDirectory = new File(directory.getCanonicalPath()); 95 } 96 catch (IOException e) { 97 mDirectory = directory; 98 } 99 100 if (format == null) { 101 throw new NullPointerException 102 ("FileLogStreamFactory date format not specified"); 103 } 104 105 mDateFormat = format; 106 107 if (extension == null) { 108 mExtension = ""; 109 } 110 else { 111 mExtension = extension; 112 } 113 } 114 115 public OutputStream openOutputStream(Date date) throws IOException { 116 if (!mDirectory.exists()) { 117 if (!mDirectory.mkdirs()) { 118 throw new IOException("Unable to create directory: \"" + 119 mDirectory + '"'); 120 } 121 } 122 123 String fileName = mDirectory.getName() + '-'; 124 synchronized (mDateFormat) { 125 fileName += mDateFormat.format(date); 126 } 127 128 File file = new File(mDirectory, fileName + mExtension); 129 return new FileOutputStream(file.getPath(), true); 130 } 131 } 132 133 | Popular Tags |