F Turner

Ranch Hand
+ Follow
since Sep 06, 2016
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by F Turner

Hi,

I'm experiencing an interesting problem. I am trying to send a file over a socket connection as a byte stream, but also send with it some control messages as text ('data incoming', number of bytes to read from the stream etc). This all works fine - when I need to read the control messages, I fill a byte buffer from the known start byte of this message until the line feed character ('\n'; UTF-8 character 10), and then convert that byte series into a string.

An interesting problem however is that the rest of the program has been written to use a BufferedReader over the socket stream, and I don't really want to change all instances of the BufferedReader's use to my slightly ad hoc string converting mechanism described above.

The reason I created the mechanism above to start with is because, if a BufferedReader and BufferedInputStream are created over the same InputStream, the buffers are not synchronised. I.e. If the two are initialised by:
and the code
is called, when the subsequent code
is called, the five bytes read by the BufferedInputStream will be read again.

Really, I believe this is a problem with individual buffering for the two streams. If I could do something like this, where the BufferedReader and BufferedInputStream share the buffer:
that would be perfect, but it doesn't look like you can.

Even if you read some bytes using the byte stream, and then create a new reader object to read some characters, the bytes for the characters seem to have been 'consumed' by the buffered input stream (not because they are read (read() is called), just because they are buffered (as far as I can tell)), and so are gone by the time that the reader tries to read them. And since I am sending files, I suspect that the input stream needs to be buffered for performance reasons.

Does anyone have any thoughts on ways to accomplish what I am looking to do - have a buffered reader and input stream over the same incoming socket connection, which are synchronised such that one starts reading where the other left off?

Thanks
4 years ago
Hi,

I have written a program responsible for submitting data to a server program and monitoring said server program to ensure that it is functioning correctly. Occasionally there is a problem and I am required to kill the server program, for which I use the Java Process API (Java.lang.Process). If running the program in a terminal window, one would kill it using Ctrl+C, so that it is able to perform clean up actions - this is necessary for the program in hand. Hence I terminate it using the OS command 'kill -SIGINT {process id}', the interrupt signal. Here is my code:



However, will this wait until the process [server program] is actually dead (i.e. all of the clean up actions have been carried out and it has finished execution), or will it only wait for the interrupt signal to be sent successfully? It is important to my program that the server is shut down before the program continues.

Thanks
4 years ago
Hi,

I am writing a program which works with epoch timestamps out of log files. There is a part of the program in which I am iterating through these timestamps, trying to find the first after a threshold, and since it is very important that there aren't any number representation errors, I would like to store the time read in as a BigDecimal object, initialised from the string in the file (unfortunately I can't use longs to store the time as the format of the string is <digits>.<digits> seconds).

The immutability of BigDecimal objects poses a problem, as, since I am checking each timestamp in turn and there is very little logic in each loop iteration, I would be creating a huge number of BigDecimal objects, and the program may in the future be extended for concurrent execution, such that many files would be processed at once. I'm therefore worried about the memory footprint of the method described.

I was initially wondering if there's a way to deallocate memory for objects in Java, such that I could deallocate the previous BigDecimal object each time I created a new one, but there doesn't seem to be. The closest I seem to be able to get is being able to request garbage collection, but will that be fast enough to offset the rate at which new BigDecimal objects are being created, and is it a bad idea to perform the request as often as in a for loop which is executed hundreds or thousands of times each second?

If anyone has any suggestions for reading in the time from the file so that it can be compared without loss of accuracy due to number representation I would be only too happy to receive them

Many thanks.
4 years ago
Hi,

My program reads in data from various different sources and needs to match it up using timestamps. Some of the timestamps were provided by a call to System.currentTimeMillis() in the application which created the files, and I need to know how accurate this is, so that I can account for any rounding errors which might have occurred. This isn't actually as obvious as it sounds, as I know that this depends on the accuracy of time measurement by the hardware, OS, and I think the JVM even has its own notion of time which is only required to be consistent with UTC every noon, but I believe that a call to currentTimeMillis() bypasses this and retrieves the time directly from the OS (is this correct?).

In principle, my question is: does System.currentTimeMillis() actually give the time accurate to the nearest millisecond on Linux, Mac OS and Windows (and since which versions - I know, for example, that Windows only used to be accurate to the nearest 15/16 milliseconds).

Thanks :)
4 years ago
Hi Rob,

Thanks very much for the help - that was exactly what I needed! I would like to understand how this works behind the scenes a little better. I originally accomplished what I was trying to achieve using reflection, but I didn't like it as a solution (the code did not seem very 'safe' against erroneous future modification when the method names were specified by strings), but how can Class::methodName be accomplished behind the scenes without using reflection? Behind the scenes, are the instance methods of an object actually represented statically in memory, and a reference to the instance is passed to them, i.e. that myMethod() for myInstance of myObject is actually myObject.myMethod(myInstance)? Sorry for such a badly articulated question :|
4 years ago
Hi,

I have three very similar functions, which effectively do the same processing, but call different functions. Some example code is shown below:

Where MyObject is an object of a user defined class, and function1(), function2() and function3() are methods of that object. I had hoped that lambda expressions and method references would allow me do to something like:


As you can probably see, my understanding of how this works is quite flaky. I have seen examples in which the argument to a method is the functional interface, and the method declared by the interface is called within the method with an implementation as a lambda expression passed in as an argument. However, I'm not entirely sure how to execute a function passed in as an argument on an object as intended above. Would anybody be able to give me some tips/fill in the gaps in my understanding?

Many thanks.
4 years ago
I was looking at my code with the debugger in Eclipse and came across some strange behaviour, as indicated below:

Would anyone be able to tell me why I am getting the <error(s)_during_the_evaluation> message at the indicated point in the code? It is as if the value for myInteger is only 'held' within the region it is assigned to the variable, which I don't think is how Java operates. Perhaps I am just missing something obvious...

Many thanks.
4 years ago
Hi, apologies for my delayed reply.

Yes, the JsonParsingException class is in one of the other JAR files (not the one which contains my application).

I have just managed to get it working - I opened the manifest file after extracting it so that I could post its contents, and the JAR files listed in the Class-Path: line were missing the extension .jar; I had found this before and thought I had added it, but obviously passed the jar command to create the jar the old manifest file.

In case it makes a difference, I also put the two JARs which I am using in the same folder as the source code when compiling it, so that in the -classpath argument to javac I only needed to give the names of said JAR files and not absolute file paths - would this have made a difference?

Thanks for the help - if you hadn't made me re-open the manifest file I wouldn't have found the problem :P
5 years ago
Hi Norm,

The program starts running fine, but then I get this:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/json/stream/JsonParsingException
       at MainClass.main(MainClass.java:323)
Caused by: java.lang.ClassNotFoundException: javax.json.stream.JsonParsingException
       at java.net.URLClassLoader.findClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       ... 1 more
5 years ago
Hello,

I've written an application which uses two other JAR files, and am trying to package it into a JAR file - something that I've never done before... I am having considerable difficulty with classpaths and where to put the various JAR files. Ideally, I would like to have the two JAR files I've used inside the JAR file for my application, but I'm not sure that my classes will be able to reference the classes within them if I do this, without custom code (this is what the documentation seems to suggest, but I may have misunderstood it). I have compiled my java files using -classpath <absolute reference to the directory in which they are stored>, which could possibly be why it isn't working now that I've moved the JARs? I don't mind just putting my application's JAR archive file in the same folder as the other two I am using - I have tried this and adjusted the Class-Path field in the manifest.mf file but with no success.

Would anybody be able to advise me on how to compile my java code (i.e. regarding the classpath), where relative to each other in the directory structure I should put my various class and JAR files, and what the manifest.mf file should contain in order to make it work?

Any help would be very much appreciated :/ Thanks!
5 years ago
Hi Campbell,

Just to clarify: I am running an operating system process and redirecting its output to a file. I was wondering if I somehow have to close this connection, but as far as I can see it is actually nothing to do with my program or the JVM, once it [the OS process] has been started.
I create a new File object in this line of code (line 2):

But do not create any writers or streams to write to it.

My thoughts were that, when you execute a command in a shell, such as:

The OS does all of the file handling for you, so I suspected that the case would be similar here. Am I correct in thinking this?
5 years ago
Thanks for the cow! I don't know what type of InputStream object will be returned in my actual program via myProc.getErrorStream(), but I'll just have to believe that that is the standard way that Java has been designed (i.e. block while there is no input but the possibility for more, only return null when there cannot be any more).

Would you mind addressing my second and third questions in the second of my posts in this thread?
5 years ago
Sorry, I clicked submit instead of preview...

My findings were just as described: that BufferedReader blocks on readLine() until there is more input to be received (looking at the API code seems to confirm this, though I only looked at it briefly), and only when the stream is closed does the while loop in ConcurrentReader exit - i.e. the BufferedReader does wait for input and doesn't move on from the 'collecting' loop as soon as it has exhausted the buffer.

Does this mean that src.close() in Program.main() calls notify/notifyAll() in the background?
5 years ago
Hi, thanks for all of the comments and help. This is basically what you just said Campbell, but I tried an experiment as Tony suggested. This was the code:

5 years ago