I have a FileStream (GLib.stdout really) and I want to pass it to a function that needs an OutputStream (Vte.Terminal.write_contents). How do you convert between them?
Create a new UnixOutputStream using the file descriptor from the stream you want. (e.g., new UnixOutputStream(stdout.fileno(), false)
Related
I’m using metalsmith and create a plugin.
I am trying to change the contents, which is a Buffer type.
This is how the metalsmith-partials plugin is doing the same thing.
// get contents string
const contents = fileData.contents.toString();
// creates a new buffer using modified content string
fileData.contents = Buffer.from(contents.replace(markdownInclude.marker, markdownInclude.markerReplacement));
So I copied this code, yet instead of creating a Buffer object, it creates a UInt8Array object.
I am aware that a Buffer is a (subclass?) of UInt8Array but I want to create a Buffer.
How can I force my statement to actually give me a Buffer?
I'm very new to Android programming. I have a code which creates a file in a designated folder and then tried to write something to it. Like below:
path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(path, "Test.xml");
Directory.CreateDirectory (path);
if (!File.Exists (path + "/" + "Test.xml")) {
File.Create (path + "/" + "Test.xml");
}
using (var streamWriter = new StreamWriter(filename, true))
{
streamWriter.WriteLine("<?xml version='1.0' encoding='utf-8'?>");
streamWriter.WriteLine ("<Apples>");
streamWriter.WriteLine ("</Apples>");
}
In line using (var streamWriter = new StreamWriter(filename, true)), I'm getting the Sharing Violation on path error.
Could someone please point me as to exactly where I'm going wrong and provide me a solution.
Thanks,
Anirban
Why do you create the file then reopen it to write to it. StreamWriter has an method that will do just that. It will create a new file if it doesn't exist.
Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.
StreamWriter could not access the file because File.Create returned a FileStream you did not consume.
As mentioned above, the File.Create is not necessary. You could also use:
using (var writer = new StreamWriter(File.Create(statusTxtPath)))
{
// do work here.
}
which will consume the file stream and close it. Whenever working with streams and most classes that interact with streams, be sure to use the using() block to ensure handles are released properly.
Ok...I have managed to resolve the issue...by using
using (var streamWriter = new StreamWriter (File.Create (path + "/" + "DoctorsList.xml")))
I'm using the ZLibDeflater to compress a file, by reading it as a stream and transforming it:
new File(filePath)
.openRead()
.transform(new ZLibDeflater())
.pipe(new File(gzipPath).openWrite())
.then(...);
As the ZLibDeflater is now deprecated, how can I convert the code to use the new GZipCodec class?
You can also use ZLIB :
new File(filePath)
.openRead()
.transform(ZLIB.decoder)
.pipe(new File(zlibPath).openWrite())
.then(...);
Gave up to early, you can simply do it using the Converter.bind() method to transform a stream:
const GZipCodec()
.encoder.bind(new File(filePath).openRead())
.pipe(new File(gzipPath).openWrite())
.then(...)
I have a site where I allow members to upload photos. In the MVC Controller I take the FormCollection as the parameter to the Action. I then read the first file as type HttpPostedFileBase. I use this to generate thumbnails. This all works fine.
In addition to allowing members to upload their own photos, I would like to use the System.Net.WebClient to import photos myself.
I am trying to generalize the method that processes the uploaded photo (file) so that it can take a general Stream object instead of the specific HttpPostedFileBase.
I am trying to base everything off of Stream since the HttpPostedFileBase has an InputStream property that contains the stream of the file and the WebClient has an OpenRead method that returns Stream.
However, by going with Stream over HttpPostedFileBase, it looks like I am loosing ContentType and ContentLength properties which I use for validating the file.
Not having worked with binary stream before, is there a way to get the ContentType and ContentLength from a Stream? Or is there a way to create a HttpPostedFileBase object using the Stream?
You're right to look at it from a raw stream perspective because then you can create one method that handles streams and therefore many scenarios from which they come.
In the file upload scenario, the stream you're acquiring is on a separate property from the content-type. Sometimes magic numbers (also a great source here) can be used to detect the data type by the stream header bytes but this might be overkill since the data is already available to you through other means (i.e. the Content-Type header, or the .ext file extension, etc).
You can measure the byte length of the stream just by virtue of reading it so you don't really need the Content-Length header: the browser just finds it useful to know what size of file to expect in advance.
If your WebClient is accessing a resource URI on the Internet, it will know the file extension like http://www.example.com/image.gif and that can be a good file type identifier.
Since the file info is already available to you, why not open up one more argument on your custom processing method to accept a content type string identifier like:
public static class Custom {
// Works with a stream from any source and a content type string indentifier.
static public void SavePicture(Stream inStream, string contentIdentifer) {
// Parse and recognize contentIdentifer to know the kind of file.
// Read the bytes of the file in the stream (while counting them).
// Write the bytes to wherever the destination is (e.g. disk)
// Example:
long totalBytesSeen = 0L;
byte[] bytes = new byte[1024]; //1K buffer to store bytes.
// Read one chunk of bytes at a time.
do
{
int num = inStream.Read(bytes, 0, 1024); // read up to 1024 bytes
// No bytes read means end of file.
if (num == 0)
break; // good bye
totalBytesSeen += num; //Actual length is accumulating.
/* Can check for "magic number" here, while reading this stream
* in the case the file extension or content-type cannot be trusted.
*/
/* Write logic here to write the byte buffer to
* disk or do what you want with them.
*/
} while (true);
}
}
Some useful filename parsing features are in the IO namespace:
using System.IO;
Use your custom method in the scenarios you mentioned like so:
From an HttpPostedFileBase instance named myPostedFile
Custom.SavePicture(myPostedFile.InputStream, myPostedFile.ContentType);
When using a WebClient instance named webClient1:
var imageFilename = "pic.gif";
var stream = webClient1.DownloadFile("http://www.example.com/images/", imageFilename)
//...
Custom.SavePicture(stream, Path.GetExtension(imageFilename));
Or even when processing a file from disk:
Custom.SavePicture(File.Open(pathToFile), Path.GetExtension(pathToFile));
Call the same custom method for any stream with a content identifer that you can parse and recognize.
how can access the binary data file(.DAT). i am using geonames API. can anyone help me?
If you are referring to the binary flat file format used by MaxMinds GeoLocation database, they offer some handy utility classes in C# and Java to access it.
http://www.maxmind.com/app/api
Assuming you are using C# (from the tag), you can use BinaryReader class to read binary data. See How to read and write to a binary file:
FileStream fs = File.Open(Environment.CurrentDirectory + #"\settings.bin", FileMode.Open);
BinaryReader reader = new BinaryReader(fs);
long number = reader.ReadInt64();
byte[] bytes = reader.ReadBytes(3);
string s = reader.ReadString();
reader.Close();
fs.Close();
Console.WriteLine(number);
foreach (byte b in bytes)
{
Console.Write("[{0}]", b);
}
Console.WriteLine();
Console.WriteLine(s);