How to read records terminated by custom separator from file in python?

There is nothing in the Python 2.x file object, or the Python 3.3 io classes, that lets you specify a custom delimiter for readline. (The for line in file is ultimately using the same code as readline.) But it’s pretty easy to build it yourself. For example: def delimited(file, delimiter=”\n”, bufsize=4096): buf=”” while True: newbuf … Read more

ObjectInputStream(socket.getInputStream()); does not work

The ObjectInputStream constructor reads data from the given InputStream. In order for this to work, you must flush the ObjectOutputStream immediately after construction (to write the initial header) before you attempt to open the ObjectInputStream. Also, if you want to send more than one object per connection, you must open the ObjectOutputStream once and use … Read more

How can I split (copy) a Stream in .NET?

I have made a SplitStream available on github and NuGet. It goes like this. using (var inputSplitStream = new ReadableSplitStream(inputSourceStream)) using (var inputFileStream = inputSplitStream.GetForwardReadOnlyStream()) using (var outputFileStream = File.OpenWrite(“MyFileOnAnyFilestore.bin”)) using (var inputSha1Stream = inputSplitStream.GetForwardReadOnlyStream()) using (var outputSha1Stream = SHA1.Create()) { inputSplitStream.StartReadAhead(); Parallel.Invoke( () => { var bytes = outputSha1Stream.ComputeHash(inputSha1Stream); var checksumSha1 = string.Join(“”, bytes.Select(x … Read more

Filesystem watcher and large files

Solution found on stackoverflow and modified it a bit. static bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return … Read more

How to read UTF8 encoded file using RandomAccessFile?

You can convert string, read by readLine to UTF8, using following code: public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile(new File(“MyFile.txt”), “r”); String line = raf.readLine(); String utf8 = new String(line.getBytes(“ISO-8859-1”), “UTF-8”); System.out.println(“Line: ” + line); System.out.println(“UTF8: ” + utf8); } Content of MyFile.txt: (UTF-8 Encoding) Привет из Украины Console … Read more

Return value of fgets()

The documentation for fgets() does not say what you think it does: From my manpage fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the … Read more