public void Copy(Context context, string fromFileName, Android.Net.Uri toUri, int bufferSize = 1024, CancellationToken?token = null) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (string.IsNullOrEmpty(fromFileName)) { throw new ArgumentNullException(nameof(fromFileName)); } if (toUri == null) { throw new ArgumentNullException(nameof(toUri)); } if (!System.IO.File.Exists(fromFileName)) { throw new System.IO.FileNotFoundException(fromFileName); } if (token == null) { token = CancellationToken.None; } var buffer = new byte[bufferSize]; using (var readStream = System.IO.File.OpenRead(fromFileName)) { using (var openFileDescriptor = context.ContentResolver.OpenFileDescriptor(toUri, "w")) { using (var fileOutputStream = new Java.IO.FileOutputStream(openFileDescriptor.FileDescriptor)) { using (var bufferedStream = new System.IO.BufferedStream(readStream)) { int count; while ((count = bufferedStream.Read(buffer, 0, bufferSize)) > 0) { if (token.Value.IsCancellationRequested) { return; } fileOutputStream.Write(buffer, 0, count); } fileOutputStream.Close(); openFileDescriptor.Close(); } } } } }
private static void copyStreamToFile(System.IO.Stream stream, string destination) { using (System.IO.BufferedStream bs = new System.IO.BufferedStream(stream)) { using (System.IO.FileStream os = System.IO.File.OpenWrite(destination)) { byte[] buffer = new byte[2 * 4096]; int nBytes; while ((nBytes = bs.Read(buffer, 0, buffer.Length)) > 0) { os.Write(buffer, 0, nBytes); } } } }
byte [] ReadData() { string s = ReadLine(); if (s.Length == 0) { throw new ResponseException("Zero length respose"); } char c = s [0]; if (c == '-') { throw new ResponseException(s.StartsWith("-ERR ") ? s.Substring(5) : s.Substring(1)); } if (c == '$') { if (s == "$-1") { return(null); } int n; if (System.Int32.TryParse(s.Substring(1), out n)) { byte [] retbuf = new byte [n]; int bytesRead = 0; do { int read = bstream.Read(retbuf, bytesRead, n - bytesRead); if (read < 1) { throw new ResponseException("Invalid termination mid stream"); } bytesRead += read; }while (bytesRead < n); if (bstream.ReadByte() != '\r' || bstream.ReadByte() != '\n') { throw new ResponseException("Invalid termination"); } return(retbuf); } throw new ResponseException("Invalid length"); } throw new ResponseException("Unexpected reply: " + s); }
public static Task<Uri> DownloadUserIcon(Uri dataUrl) { Uri tmp; if (_cacheDictionary.TryGetValue(dataUrl, out tmp)) return Task.Factory.StartNew(obj => (Uri)obj, tmp); else return Task.Factory.StartNew(() => { var req = System.Net.HttpWebRequest.Create(dataUrl); var res = (System.Net.HttpWebResponse)req.GetResponse(); var cacheFilePath = System.IO.Path.GetTempFileName(); int recieveByte; byte[] buff = new byte[1024]; using (var nstrm = new System.IO.BufferedStream(res.GetResponseStream())) using (var fstrm = System.IO.File.OpenWrite(cacheFilePath)) while ((recieveByte = nstrm.Read(buff, 0, buff.Length)) > 0) fstrm.Write(buff, 0, recieveByte); return new Uri(cacheFilePath); }); }
private static void copyStreamToFile(System.IO.Stream stream, string destination) { System.Diagnostics.Debug.Assert(stream != null); if (destination == null || destination == string.Empty) { return; } using (System.IO.BufferedStream bs = new System.IO.BufferedStream(stream)) { using (System.IO.FileStream os = System.IO.File.OpenWrite(destination)) { byte[] buffer = new byte[2 * 4096]; int nBytes; while ((nBytes = bs.Read(buffer, 0, buffer.Length)) > 0) { os.Write(buffer, 0, nBytes); } } } }
public static System.IO.MemoryStream GetFile(AmazonS3 s3Client, string filekey) { using (s3Client) { S3_KEY = filekey; System.IO.MemoryStream file = new System.IO.MemoryStream(); try { GetObjectResponse r = s3Client.GetObject(new GetObjectRequest() { BucketName = BUCKET_NAME, Key = S3_KEY }); try { long transferred = 0L; System.IO.BufferedStream stream2 = new System.IO.BufferedStream(r.ResponseStream); byte[] buffer = new byte[0x2000]; int count = 0; while ((count = stream2.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, count); } } finally { } return(file); } catch (AmazonS3Exception) { //Show exception } } return(null); }
/// <summary> /// Save configuration to a XML file /// </summary> /// <param name="filename">The full path filename to save configuration</param> public void SaveToXml(string filename) { System.IO.BufferedStream xmlStream = null; byte [] xmlData; const int bufferSize = 4096; XmlDocument doc; #region Create doc, loading default xml // Load default xml from resource xmlStream = new System.IO.BufferedStream( Assembly.GetExecutingAssembly().GetManifestResourceStream("Report.Layout.Complex.DefaultLabelLayout.xml"), bufferSize); xmlData = new byte[xmlStream.Length]; while (xmlStream.Position < xmlStream.Length) xmlStream.Read((byte[]) xmlData, (int) xmlStream.Position, (int) Math.Min(bufferSize, xmlStream.Length - xmlStream.Position)); xmlStream.Close(); string strDefaultXml = System.Text.Encoding.UTF8.GetString(xmlData); strDefaultXml = strDefaultXml.Substring(strDefaultXml.IndexOf('<')); doc = new XmlDocument(); doc.LoadXml(strDefaultXml); #endregion SaveToXml(doc, doc["LabelLayout"]["ItemLayout"]); doc.Save(filename); }
/** * Reads in the binary file specified. * * <p>If there are any problems reading in the file, it gets classified as unidentified, * with an explanatory warning message. */ private void readFile() { //If file is not readable or is empty, then it gets classified //as unidentified (with an explanatory warning) if( !file.Exists ) { this.SetErrorIdentification(); this.SetIdentificationWarning("File does not exist"); return; } //TODO: figure out how to port this /* if( !file.canRead() ) { this.SetErrorIdentification(); this.SetIdentificationWarning("File cannot be read"); return; } */ if (System.IO.Directory.Exists(file.FullName)) { this.SetErrorIdentification(); this.SetIdentificationWarning("This is a directory, not a file"); return; } //FileInputStream binStream; System.IO.FileStream binStream; try { binStream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open); //FileInputStream(file); } catch (System.IO.FileNotFoundException) { this.SetErrorIdentification(); this.SetIdentificationWarning("File disappeared or cannot be read"); return; } try { int numBytes = 100; //binStream.available(); if (numBytes > 0) { //BufferedInputStream buffStream = new BufferedInputStream(binStream); System.IO.BufferedStream buffStream = new System.IO.BufferedStream(binStream); fileBytes = new byte[numBytes]; int len = buffStream.Read(fileBytes, 0, numBytes); if(len != numBytes) { //This means that all bytes were not successfully read this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: "+ len.ToString() + " bytes read from file when " + numBytes.ToString() + " were expected"); } else if(len != -1) { //This means that the end of the file was not reached this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: Unable to read to the end"); } else { myNumBytes = (long) numBytes; } buffStream.Close(); } else { //If file is empty , status is error this.SetErrorIdentification(); myNumBytes = 0L; this.SetIdentificationWarning("Zero-length file"); } binStream.Close(); isRandomAccess = false; } catch(System.IO.IOException e) { this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: " + e.ToString()); } catch(System.OutOfMemoryException) { try { myRandomAccessFile = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open); //RandomAccessFile(file,"r"); isRandomAccess = true; //record the file size myNumBytes = myRandomAccessFile.Length; //try reading in a buffer myRandomAccessFile.Seek(0, System.IO.SeekOrigin.Begin); //(0L); bool tryAgain = true; while(tryAgain) { try { fileBytes = new byte[(int)randomFileBufferSize]; myRandomAccessFile.Read(fileBytes, 0, randomFileBufferSize); // .read(fileBytes); tryAgain = false; } catch(OutOfMemoryException e4) { randomFileBufferSize = randomFileBufferSize/RAF_BUFFER_REDUCTION_FACTOR; if(randomFileBufferSize< MIN_RAF_BUFFER_SIZE) { throw e4; } } } myRAFoffset = 0L; } catch (System.IO.FileNotFoundException) { this.SetErrorIdentification(); this.SetIdentificationWarning("File disappeared or cannot be read"); } catch(Exception e2) { try { myRandomAccessFile.Close(); } catch(System.IO.IOException) { } this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: " + e2.ToString()); } } }
public static System.IO.MemoryStream GetFile(AmazonS3 s3Client, string filekey) { using (s3Client) { S3_KEY = filekey; System.IO.MemoryStream file = new System.IO.MemoryStream(); try { GetObjectResponse r = s3Client.GetObject(new GetObjectRequest() { BucketName = BUCKET_NAME, Key = S3_KEY }); try { long transferred = 0L; System.IO.BufferedStream stream2 = new System.IO.BufferedStream(r.ResponseStream); byte[] buffer = new byte[0x2000]; int count = 0; while ((count = stream2.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, count); } } finally { } return file; } catch (AmazonS3Exception) { //Show exception } } return null; }
/** * Reads in the binary file specified. * * <p>If there are any problems reading in the file, it gets classified as unidentified, * with an explanatory warning message. */ private void readFile() { //If file is not readable or is empty, then it gets classified //as unidentified (with an explanatory warning) if (!file.Exists) { this.SetErrorIdentification(); this.SetIdentificationWarning("File does not exist"); return; } //TODO: figure out how to port this /* * if( !file.canRead() ) * { * this.SetErrorIdentification(); * this.SetIdentificationWarning("File cannot be read"); * return; * } */ if (System.IO.Directory.Exists(file.FullName)) { this.SetErrorIdentification(); this.SetIdentificationWarning("This is a directory, not a file"); return; } //FileInputStream binStream; System.IO.FileStream binStream; try { binStream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open); //FileInputStream(file); } catch (System.IO.FileNotFoundException) { this.SetErrorIdentification(); this.SetIdentificationWarning("File disappeared or cannot be read"); return; } try { int numBytes = 100; //binStream.available(); if (numBytes > 0) { //BufferedInputStream buffStream = new BufferedInputStream(binStream); System.IO.BufferedStream buffStream = new System.IO.BufferedStream(binStream); fileBytes = new byte[numBytes]; int len = buffStream.Read(fileBytes, 0, numBytes); if (len != numBytes) { //This means that all bytes were not successfully read this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: " + len.ToString() + " bytes read from file when " + numBytes.ToString() + " were expected"); } else if (len != -1) { //This means that the end of the file was not reached this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: Unable to read to the end"); } else { myNumBytes = (long)numBytes; } buffStream.Close(); } else { //If file is empty , status is error this.SetErrorIdentification(); myNumBytes = 0L; this.SetIdentificationWarning("Zero-length file"); } binStream.Close(); isRandomAccess = false; } catch (System.IO.IOException e) { this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: " + e.ToString()); } catch (System.OutOfMemoryException) { try { myRandomAccessFile = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open); //RandomAccessFile(file,"r"); isRandomAccess = true; //record the file size myNumBytes = myRandomAccessFile.Length; //try reading in a buffer myRandomAccessFile.Seek(0, System.IO.SeekOrigin.Begin); //(0L); bool tryAgain = true; while (tryAgain) { try { fileBytes = new byte[(int)randomFileBufferSize]; myRandomAccessFile.Read(fileBytes, 0, randomFileBufferSize); // .read(fileBytes); tryAgain = false; } catch (OutOfMemoryException e4) { randomFileBufferSize = randomFileBufferSize / RAF_BUFFER_REDUCTION_FACTOR; if (randomFileBufferSize < MIN_RAF_BUFFER_SIZE) { throw e4; } } } myRAFoffset = 0L; } catch (System.IO.FileNotFoundException) { this.SetErrorIdentification(); this.SetIdentificationWarning("File disappeared or cannot be read"); } catch (Exception e2) { try { myRandomAccessFile.Close(); } catch (System.IO.IOException) { } this.SetErrorIdentification(); this.SetIdentificationWarning("Error reading file: " + e2.ToString()); } } }
byte [] ReadData() { string s = ReadLine(); Log("S", s); if (s.Length == 0) { throw new ResponseException("Zero length respose"); } char c = s [0]; if (c == '-') { throw new ResponseException(s.StartsWith("-ERR ") ? s.Substring(5) : s.Substring(1)); } if (c == '$') { if (s == "$-1") { return(null); } int n; if (System.Int32.TryParse(s.Substring(1), out n)) { byte [] retbuf = new byte [n]; int bytesRead = 0; do { int read = bstream.Read(retbuf, bytesRead, n - bytesRead); if (read < 1) { throw new ResponseException("Invalid termination mid stream"); } bytesRead += read; }while (bytesRead < n); if (bstream.ReadByte() != '\r' || bstream.ReadByte() != '\n') { throw new ResponseException("Invalid termination"); } return(retbuf); } throw new ResponseException("Invalid length"); } /* don't treat arrays here because only one element works -- use DataArray! * //returns the number of matches * if (c == '*') { * int n; * if (Int32.TryParse(s.Substring(1), out n)) * return n <= 0 ? new byte [0] : ReadData(); * * throw new ResponseException ("Unexpected length parameter" + r); * } */ throw new ResponseException("Unexpected reply: " + s); }