use of java.io.FileOutputStream in project jetty.project by eclipse.
the class RolloverFileOutputStream method setFile.
/* ------------------------------------------------------------ */
private synchronized void setFile() throws IOException {
// Check directory
File file = new File(_filename);
_filename = file.getCanonicalPath();
file = new File(_filename);
File dir = new File(file.getParent());
if (!dir.isDirectory() || !dir.canWrite())
throw new IOException("Cannot write log directory " + dir);
Date now = new Date();
// Is this a rollover file?
String filename = file.getName();
int i = filename.toLowerCase(Locale.ENGLISH).indexOf(YYYY_MM_DD);
if (i >= 0) {
file = new File(dir, filename.substring(0, i) + _fileDateFormat.format(now) + filename.substring(i + YYYY_MM_DD.length()));
}
if (file.exists() && !file.canWrite())
throw new IOException("Cannot write log file " + file);
// Do we need to change the output stream?
if (out == null || !file.equals(_file)) {
// Yep
_file = file;
if (!_append && file.exists())
file.renameTo(new File(file.toString() + "." + _fileBackupFormat.format(now)));
OutputStream oldOut = out;
out = new FileOutputStream(file.toString(), _append);
if (oldOut != null)
oldOut.close();
//if(log.isDebugEnabled())log.debug("Opened "+_file);
}
}
use of java.io.FileOutputStream in project jetty.project by eclipse.
the class JarResource method copyTo.
/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
if (!exists())
return;
if (LOG.isDebugEnabled())
LOG.debug("Extract " + this + " to " + directory);
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
if (LOG.isDebugEnabled())
LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
URLConnection c = jarFileURL.openConnection();
c.setUseCaches(false);
try (InputStream is = c.getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
boolean shouldExtract;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
// is the subentry really a dir?
if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
subEntryIsDir = true;
//extract it.
if (subEntryIsDir) {
//if it is a subdirectory we are looking for, then we
//are looking to extract its contents into the target
//directory. Remove the name of the subdirectory so
//that we don't wind up creating it too.
entryName = entryName.substring(subEntryName.length());
if (!entryName.equals("")) {
//the entry is
shouldExtract = true;
} else
shouldExtract = false;
} else
shouldExtract = true;
} else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
//there is a particular entry we are looking for, and this one
//isn't it
shouldExtract = false;
} else {
//we are extracting everything
shouldExtract = true;
}
if (!shouldExtract) {
if (LOG.isDebugEnabled())
LOG.debug("Skipping entry: " + entryName);
continue;
}
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
if (LOG.isDebugEnabled())
LOG.debug("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
// Make directory
if (!file.exists())
file.mkdirs();
} else {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
}
}
if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
Manifest manifest = jin.getManifest();
if (manifest != null) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
use of java.io.FileOutputStream in project che by eclipse.
the class DeltaProcessingState method saveExternalLibTimeStamps.
public void saveExternalLibTimeStamps() throws CoreException {
if (this.externalTimeStamps == null)
return;
// cleanup to avoid any leak ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=244849 )
HashSet toRemove = new HashSet();
if (this.roots != null) {
Enumeration keys = this.externalTimeStamps.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (this.roots.get(key) == null) {
toRemove.add(key);
}
}
}
File timestamps = getTimeStampsFile();
DataOutputStream out = null;
try {
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(timestamps)));
out.writeInt(this.externalTimeStamps.size() - toRemove.size());
Iterator entries = this.externalTimeStamps.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
IPath key = (IPath) entry.getKey();
if (!toRemove.contains(key)) {
out.writeUTF(key.toPortableString());
Long timestamp = (Long) entry.getValue();
out.writeLong(timestamp.longValue());
}
}
} catch (IOException e) {
//$NON-NLS-1$
IStatus status = new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, IStatus.ERROR, "Problems while saving timestamps", e);
throw new CoreException(status);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// nothing we can do: ignore
}
}
}
}
use of java.io.FileOutputStream in project che by eclipse.
the class ResetTest method testResetSoft.
@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testResetSoft(GitConnectionFactory connectionFactory) throws Exception {
//given
GitConnection connection = connectToGitRepositoryWithContent(connectionFactory, repository);
File aaa = addFile(connection, "aaa", "aaa\n");
FileOutputStream fos = new FileOutputStream(new File(connection.getWorkingDir(), "README.txt"));
fos.write("MODIFIED\n".getBytes());
fos.flush();
fos.close();
String initMessage = connection.log(LogParams.create()).getCommits().get(0).getMessage();
connection.add(AddParams.create(new ArrayList<>(singletonList("."))));
connection.commit(CommitParams.create("add file"));
//when
connection.reset(ResetParams.create("HEAD^", ResetRequest.ResetType.SOFT));
//then
assertEquals(connection.log(LogParams.create()).getCommits().get(0).getMessage(), initMessage);
assertTrue(aaa.exists());
assertEquals(connection.status(StatusFormat.SHORT).getAdded().get(0), "aaa");
assertEquals(connection.status(StatusFormat.SHORT).getChanged().get(0), "README.txt");
assertEquals(Files.toString(new File(connection.getWorkingDir(), "README.txt"), Charsets.UTF_8), "MODIFIED\n");
}
use of java.io.FileOutputStream in project che by eclipse.
the class ResetTest method testResetHard.
@Test(dataProvider = "GitConnectionFactory", dataProviderClass = org.eclipse.che.git.impl.GitConnectionFactoryProvider.class)
public void testResetHard(GitConnectionFactory connectionFactory) throws Exception {
//given
GitConnection connection = connectToGitRepositoryWithContent(connectionFactory, repository);
File aaa = addFile(connection, "aaa", "aaa\n");
FileOutputStream fos = new FileOutputStream(new File(connection.getWorkingDir(), "README.txt"));
fos.write("MODIFIED\n".getBytes());
fos.flush();
fos.close();
String initMessage = connection.log(LogParams.create()).getCommits().get(0).getMessage();
connection.add(AddParams.create(new ArrayList<>(singletonList("."))));
connection.commit(CommitParams.create("add file"));
//when
connection.reset(ResetParams.create("HEAD^", ResetRequest.ResetType.HARD));
//then
assertEquals(connection.log(LogParams.create()).getCommits().get(0).getMessage(), initMessage);
assertFalse(aaa.exists());
assertTrue(connection.status(StatusFormat.SHORT).isClean());
assertEquals(CONTENT, Files.toString(new File(connection.getWorkingDir(), "README.txt"), Charsets.UTF_8));
}
Aggregations