/// <exception cref="System.IO.IOException"></exception> protected internal static void CopyFile(FilePath src, FilePath dst) { FileInputStream fis = new FileInputStream(src); try { FileOutputStream fos = new FileOutputStream(dst); try { byte[] buf = new byte[4096]; int r; while ((r = fis.Read(buf)) > 0) { fos.Write(buf, 0, r); } } finally { fos.Close(); } } finally { fis.Close(); } }
public virtual void TestReadWriteMergeMsg() { NUnit.Framework.Assert.AreEqual(db.ReadSquashCommitMsg(), null); NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, Constants.SQUASH_MSG).Exists ()); db.WriteSquashCommitMsg(squashMsg); NUnit.Framework.Assert.AreEqual(squashMsg, db.ReadSquashCommitMsg()); NUnit.Framework.Assert.AreEqual(Read(new FilePath(db.Directory, Constants.SQUASH_MSG )), squashMsg); db.WriteSquashCommitMsg(null); NUnit.Framework.Assert.AreEqual(db.ReadSquashCommitMsg(), null); NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, Constants.SQUASH_MSG).Exists ()); FileOutputStream fos = new FileOutputStream(new FilePath(db.Directory, Constants. SQUASH_MSG)); try { fos.Write(Sharpen.Runtime.GetBytesForString(squashMsg, Constants.CHARACTER_ENCODING )); } finally { fos.Close(); } NUnit.Framework.Assert.AreEqual(db.ReadSquashCommitMsg(), squashMsg); }
/// <exception cref="System.IO.IOException"></exception> public static void CopyStreamToFile(InputStream @is, FilePath file) { OutputStream os = new FileOutputStream(file); int n; byte[] buffer = new byte[16384]; while ((n = @is.Read(buffer)) > -1) { os.Write(buffer, 0, n); } os.Close(); @is.Close(); }
/// <summary> /// Saves the contents of a <code>byte[]</code> to the specified /// <see cref="Sharpen.FilePath"/> /// . /// </summary> /// <exception cref="System.IO.IOException"/> public static void SaveBytes(FilePath file, sbyte[] bytes) { FileOutputStream stream = null; try { stream = new FileOutputStream(file); stream.Write(bytes); } finally { if (stream != null) { stream.Close(); } } }
public virtual void TestReadWriteMergeHeads() { NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads(), null); db.WriteMergeHeads(Arrays.AsList(ObjectId.ZeroId, ObjectId.FromString(sampleId))); NUnit.Framework.Assert.AreEqual(Read(new FilePath(db.Directory, "MERGE_HEAD")), "0000000000000000000000000000000000000000\n1c6db447abdbb291b25f07be38ea0b1bf94947c5\n" ); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads().Count, 2); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads()[0], ObjectId.ZeroId); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads()[1], ObjectId.FromString(sampleId )); // same test again, this time with lower-level io FileOutputStream fos = new FileOutputStream(new FilePath(db.Directory, "MERGE_HEAD" )); try { fos.Write(Sharpen.Runtime.GetBytesForString("0000000000000000000000000000000000000000\n1c6db447abdbb291b25f07be38ea0b1bf94947c5\n" , Constants.CHARACTER_ENCODING)); } finally { fos.Close(); } NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads().Count, 2); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads()[0], ObjectId.ZeroId); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads()[1], ObjectId.FromString(sampleId )); db.WriteMergeHeads(Collections.EmptyList<ObjectId> ()); NUnit.Framework.Assert.AreEqual(Read(new FilePath(db.Directory, "MERGE_HEAD")), string.Empty ); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads(), null); fos = new FileOutputStream(new FilePath(db.Directory, "MERGE_HEAD")); try { fos.Write(Sharpen.Runtime.GetBytesForString(sampleId, Constants.CHARACTER_ENCODING )); } finally { fos.Close(); } NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads().Count, 1); NUnit.Framework.Assert.AreEqual(db.ReadMergeHeads()[0], ObjectId.FromString(sampleId )); }
protected internal override RandomAccessReader CreateReader(sbyte[] bytes) { try { // Unit tests can create multiple readers in the same test, as long as they're used one after the other DeleteTempFile(); _tempFile = FilePath.CreateTempFile("metadata-extractor-test-", ".tmp"); FileOutputStream stream = new FileOutputStream(_tempFile); stream.Write(bytes); stream.Close(); _randomAccessFile = new RandomAccessFile(_tempFile, "r"); return new RandomAccessFileReader(_randomAccessFile); } catch (IOException) { NUnit.Framework.Assert.Fail("Unable to create temp file"); return null; } }
/// <exception cref="System.IO.FileNotFoundException"></exception> /// <exception cref="System.IO.IOException"></exception> private void SetupReflog(string logName, byte[] data) { FilePath logfile = new FilePath(db.Directory, logName); if (!logfile.GetParentFile().Mkdirs() && !logfile.GetParentFile().IsDirectory()) { throw new IOException("oops, cannot create the directory for the test reflog file" + logfile); } FileOutputStream fileOutputStream = new FileOutputStream(logfile); try { fileOutputStream.Write(data); } finally { fileOutputStream.Close(); } }
/// <summary>Compile JavaScript source.</summary> /// <remarks>Compile JavaScript source.</remarks> public virtual void ProcessSource(string[] filenames) { for (int i = 0; i != filenames.Length; ++i) { string filename = filenames[i]; if (!filename.EndsWith(".js")) { AddError("msg.extension.not.js", filename); return; } FilePath f = new FilePath(filename); string source = ReadSource(f); if (source == null) { return; } string mainClassName = targetName; if (mainClassName == null) { string name = f.GetName(); string nojs = Sharpen.Runtime.Substring(name, 0, name.Length - 3); mainClassName = GetClassName(nojs); } if (targetPackage.Length != 0) { mainClassName = targetPackage + "." + mainClassName; } object[] compiled = compiler.CompileToClassFiles(source, filename, 1, mainClassName); if (compiled == null || compiled.Length == 0) { return; } FilePath targetTopDir = null; if (destinationDir != null) { targetTopDir = new FilePath(destinationDir); } else { string parent = f.GetParent(); if (parent != null) { targetTopDir = new FilePath(parent); } } for (int j = 0; j != compiled.Length; j += 2) { string className = (string)compiled[j]; byte[] bytes = (byte[])compiled[j + 1]; FilePath outfile = GetOutputFile(targetTopDir, className); try { FileOutputStream os = new FileOutputStream(outfile); try { os.Write(bytes); } finally { os.Close(); } } catch (IOException ioe) { AddFormatedError(ioe.ToString()); } } } }
/// <exception cref="System.IO.IOException"></exception> private NGit.Storage.File.ReflogWriter Log(string refName, byte[] rec) { FilePath log = LogFor(refName); bool write = forceWrite || (IsLogAllRefUpdates() && ShouldAutoCreateLog(refName)) || log.IsFile(); if (!write) { return this; } WriteConfig wc = GetRepository().GetConfig().Get(WriteConfig.KEY); FileOutputStream @out; try { @out = new FileOutputStream(log, true); } catch (FileNotFoundException err) { FilePath dir = log.GetParentFile(); if (dir.Exists()) { throw; } if (!dir.Mkdirs() && !dir.IsDirectory()) { throw new IOException(MessageFormat.Format(JGitText.Get().cannotCreateDirectory, dir)); } @out = new FileOutputStream(log, true); } try { if (wc.GetFSyncRefFiles()) { FileChannel fc = @out.GetChannel(); ByteBuffer buf = ByteBuffer.Wrap(rec); while (0 < buf.Remaining()) { fc.Write(buf); } fc.Force(true); } else { @out.Write(rec); } } finally { @out.Close(); } return this; }
public bool StoreBlob(byte[] data, BlobKey outKey) { BlobKey newKey = KeyForBlob(data); outKey.SetBytes(newKey.GetBytes()); string path = PathForKey(outKey); FilePath file = new FilePath(path); if (file.CanRead()) { return true; } FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.Write(data); } catch (FileNotFoundException e) { Log.E(Database.Tag, "Error opening file for output", e); return false; } catch (IOException ioe) { Log.E(Database.Tag, "Error writing to file", ioe); return false; } finally { if (fos != null) { try { fos.Close(); } catch (IOException) { } } } // ignore return true; }
public bool StoreBlobStream(Stream inputStream, out BlobKey outKey) { FilePath tmp = null; try { tmp = FilePath.CreateTempFile(TmpFilePrefix, TmpFileExtension, new FilePath(this.path)); FileOutputStream fos = new FileOutputStream(tmp); byte[] buffer = new byte[65536]; int lenRead = ((InputStream)inputStream).Read(buffer); while (lenRead > 0) { fos.Write(buffer, 0, lenRead); lenRead = ((InputStream)inputStream).Read(buffer); } inputStream.Close(); fos.Close(); } catch (IOException e) { Log.E(Database.Tag, "Error writing blog to tmp file", e); outKey = null; return false; } outKey = KeyForBlobFromFile(tmp); var keyPath = PathForKey(outKey); var file = new FilePath(keyPath); if (file.CanRead()) { // object with this hash already exists, we should delete tmp file and return true tmp.Delete(); } else { // does not exist, we should rename tmp file to this name tmp.RenameTo(file); } return true; }
/// <exception cref="System.IO.IOException"></exception> private void WriteToFile(FilePath actFile, string @string) { FileOutputStream fos = null; try { fos = new FileOutputStream(actFile); fos.Write(Sharpen.Runtime.GetBytesForString(@string, "UTF-8")); fos.Close(); } finally { if (fos != null) { fos.Close(); } } }
/// <exception cref="System.IO.IOException"></exception> private FilePath CreateFile(byte[] content) { trash.Mkdirs(); FilePath f = FilePath.CreateTempFile(GetType().FullName, null, trash); FileOutputStream os = new FileOutputStream(f, true); try { os.Write(content); } finally { os.Close(); } return f; }
/// <exception cref="Com.Drew.Metadata.MetadataException"/> /// <exception cref="System.IO.IOException"/> public virtual void WriteThumbnail([NotNull] string filename) { sbyte[] data = _thumbnailData; if (data == null) { throw new MetadataException("No thumbnail data exists."); } FileOutputStream stream = null; try { stream = new FileOutputStream(filename); stream.Write(data); } finally { if (stream != null) { stream.Close(); } } }
/// <exception cref="System.IO.IOException"></exception> private void Append(FilePath f, byte b) { FileOutputStream os = new FileOutputStream(f, true); try { os.Write(b); } finally { os.Close(); } }
/// <exception cref="System.IO.IOException"></exception> private void Write(ObjectId id, byte[] data) { FilePath path = Path(id); FileUtils.Mkdirs(path.GetParentFile()); FileOutputStream @out = new FileOutputStream(path); try { @out.Write(data); } finally { @out.Close(); } }
public virtual void TestReadLoosePackedRef() { Ref @ref = db.GetRef("refs/heads/master"); NUnit.Framework.Assert.AreEqual(RefStorage.PACKED, @ref.GetStorage()); FileOutputStream os = new FileOutputStream(new FilePath(db.Directory, "refs/heads/master" )); os.Write(Sharpen.Runtime.GetBytesForString(@ref.GetObjectId().Name)); os.Write('\n'); os.Close(); @ref = db.GetRef("refs/heads/master"); NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, @ref.GetStorage()); }
/// <exception cref="System.IO.IOException"></exception> private FilePath AddToWorkDir(string path, string content) { FilePath f = new FilePath(db.WorkTree, path); FileOutputStream fos = new FileOutputStream(f); try { fos.Write(Sharpen.Runtime.GetBytesForString(content, Constants.CHARACTER_ENCODING )); return f; } finally { fos.Close(); } }
/// <exception cref="System.IO.IOException"></exception> internal virtual void OpenIndex(ProgressMonitor pm) { if (this.index != null) { return; } if (this.tmpIdx == null) { this.tmpIdx = FilePath.CreateTempFile("jgit-walk-", ".idx"); } else { if (this.tmpIdx.IsFile()) { try { this.index = PackIndex.Open(this.tmpIdx); return; } catch (FileNotFoundException) { } } } // Fall through and get the file. WalkRemoteObjectDatabase.FileStream s; s = this.connection.Open("pack/" + this.idxName); pm.BeginTask("Get " + Sharpen.Runtime.Substring(this.idxName, 0, 12) + "..idx", s .length < 0 ? ProgressMonitor.UNKNOWN : (int)(s.length / 1024)); try { FileOutputStream fos = new FileOutputStream(this.tmpIdx); try { byte[] buf = new byte[2048]; int cnt; while (!pm.IsCancelled() && (cnt = [email protected](buf)) >= 0) { fos.Write(buf, 0, cnt); pm.Update(cnt / 1024); } } finally { fos.Close(); } } catch (IOException err) { FileUtils.Delete(this.tmpIdx); throw; } finally { [email protected](); } pm.EndTask(); if (pm.IsCancelled()) { FileUtils.Delete(this.tmpIdx); return; } try { this.index = PackIndex.Open(this.tmpIdx); } catch (IOException e) { FileUtils.Delete(this.tmpIdx); throw; } }