Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Boz0r
Sep 7, 2006
The Rocketship in action.

csammis posted:

What I meant was, didn't calling something "win" used to be probatable?

There, noone will ever know.



How do I execute an external application with or without parameters?

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boz0r posted:

There, noone will ever know.

How do I execute an external application with or without parameters?

code:
Runtime.getRuntime().exec(new String[]{ "echo", "Hello,", "World!" }).waitFor();

1337JiveTurkey
Feb 17, 2005

Boz0r posted:

How do I execute an external application with or without parameters?

Runtime.exec() runs the command given with cmdarray being the command name and the command line arguments passed to it. There's also an optional array of environment variables and an optional directory to run it in. The return value is an object used to control the spawned process.

ProcessBuilder is an object which you can set everything for beforehand and then just call start() repeatedly to spawn any number of processes.

LakeMalcom
Jul 3, 2000

It's raining on prom night.
you can also do

code:
header = new int[] { mapVersion[0], mapVersion[1], mapVersion[2] };
for that array stuff.

covener
Jan 10, 2004

You know, for kids!

1337JiveTurkey posted:

Runtime.exec() runs the command given with cmdarray being the command name and the command line arguments passed to it. There's also an optional array of environment variables and an optional directory to run it in. The return value is an object used to control the spawned process.

Be sure to drain stdout and stderr from your forked process, or it could block and hang until you do.

Boz0r
Sep 7, 2006
The Rocketship in action.
I've got this function to convert a byte array into a single integer:

code:
 
    public static int revConvert(int[] array, int radix) {
        int ret = 0;
        for(int i = array.length - 1; i >= 0; i--) {
            if (i!=0) {
                ret += array[i]*(Math.pow(radix,(i)));
            }
            else 
                ret += array[i];
        }
        return ret;
    }
but it doesn't really work properly with negative numbers. I extract {00, 60, ff, ff}(Which has to be reversed to get the proper order) from a file, and instead of giving me the number I want(-40960), I get something useless. How would I go about fixing this, or is there a library class that does this?

Boz0r fucked around with this message at 19:41 on Sep 23, 2008

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
Do you need the radix? I wrote this in like 10s and it works well:

code:
  1 public class B {
  2     public static void main(String [] args) {
  3         System.out.println(revConvert(new int [] {0x00, 0x60, 0xff, 0xff}));
  4     }
  5
  6
  7     public static int revConvert(int[] array) {
  8         int ret = 0;
  9         for(int i = array.length - 1; i >= 0; i--) {
 10             ret = ret << 8;
 11             ret += array[i];
 12         }
 13         return ret;
 14     }
 15 }

Boz0r
Sep 7, 2006
The Rocketship in action.

TRex EaterofCars posted:

Do you need the radix? I wrote this in like 10s and it works well:

I don't know if I need the radix, as I read both 1, 2 and 4 bytes, but this seems to work. Next problem I encountered is, I get the ints from my inputstream in a retarded way:

code:
    
public static int[] readBytes(int numBytes, DataInputStream input) {
        int[] ret = new int[numBytes];
        for (int i = 0; i < numBytes; i++){
            try {
                ret[i] = input.readByte();               
            }
            catch (IOException e) {
                System.out.println("IOException");
            }
        }
        return ret;
    }
When I read the bytes, I don't get them in hex, I get the {00, 60, ff, ff} like {0, 96, -1, -1}. I'd like to work with them in hex, but I haven't really found a way to do that.

Also, how do I define a parameter to be an array of any primitive type instead of just one?

Boz0r fucked around with this message at 20:33 on Sep 23, 2008

csammis
Aug 26, 2003

Mental Institution

Boz0r posted:

When I read the bytes, I don't get them in hex, I get the {00, 60, ff, ff} like {0, 96, -1, -1}. I'd like to work with them in hex, but I haven't really found a way to do that.

Um, this by itself is meaningless; -1 == 0xFF == 1111 1111 to the computer, numbers are numbers are numbers and the base doesn't matter. If you mean display them in the debugger as hexadecimal, that's one thing. If you mean put them into strings so that the characters represent the number in hexadecimal, that's another. What do you want to do?

Boz0r posted:

Also, how do I define a parameter to be an array of any primitive type instead of just one?

In Java, I don't think you can. The closest you can get is an array of object and let the primitive types get autoboxed.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

covener posted:

Be sure to drain stdout and stderr from your forked process, or it could block and hang until you do.

I was assuming that the subprocess would just inherit your stdout and stderr, but you're right, it doesn't. Hmm. In that case, I would strongly suggest not invoking any processes which create significant amounts of output unless you're planning to parse that output.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

csammis posted:

In Java, I don't think you can. The closest you can get is an array of Object and let the primitive types get autoboxed.

All Java array types are subtypes of Object, so if you really need to do this, you can just pass in an Object. But really, what are you trying to do?

Boz0r
Sep 7, 2006
The Rocketship in action.

csammis posted:

What do you want to do?

I don't really know, actually. I guess it doesn't really matter, but my problem right now is, if I use the method TRex EaterofCars wrote before with {00, 60, ff, ff} and {0, 96, -1, -1} I get two different results, with the {0, 96, -1, -1} result being wrong.

rjmccall posted:

But really, what are you trying to do?

I'm trying to make a method to reverse an array. So far I've written this:

code:
     
public static void reverse(int[] b) {
    int left  = 0;
    int right = b.length-1;
  
    while (left < right) {
        int temp = b[left]; 
        b[left]  = b[right]; 
        b[right] = temp;
     
        left++;
        right--;
    }
}
and I'd like to be able to reverse an array of any primitive type.

Boz0r fucked around with this message at 20:58 on Sep 23, 2008

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Boz0r posted:

I don't really know, actually. I guess it doesn't really matter, but my problem right now is, if I use the method TRex EaterofCars wrote before with {00, 60, ff, ff} and {0, 96, -1, -1} I get two different results, with the {0, 96, -1, -1} result being wrong.

Yes because -1 is represented as 0xffffffff, bit shifting with that number will obviously produce wrong results. Try this instead:
code:
  7     public static int revConvert(int[] array) {
  8         int ret = 0;
  9         for(int i = array.length - 1; i >= 0; i--) {
 10             ret = ret << 8;
 11             ret |= array[i] & 0xff;
 12         }
 13         return ret;
 14     }

pearofducks
Jan 21, 2003

quack?
I'm goofing around making a network game (lets call it tictactoe for now), and one player has to wait for the other to take their turn. To do this I have the waiting player sit in an infinite loop checking the input buffer to see if it has data yet, even with low thread priority and a big wait time this still sucks CPU like mad and makes a laptop take off. Is there anything that will let me get away from this loop? Maybe something that pushes the data instead of needing it to be constantly polled?

lamentable dustman
Apr 13, 2007

🏆🏆🏆

pearofducks posted:

I'm goofing around making a network game (lets call it tictactoe for now), and one player has to wait for the other to take their turn. To do this I have the waiting player sit in an infinite loop checking the input buffer to see if it has data yet, even with low thread priority and a big wait time this still sucks CPU like mad and makes a laptop take off. Is there anything that will let me get away from this loop? Maybe something that pushes the data instead of needing it to be constantly polled?

Try thread wait(). Set it to poll every half second or so. Co-worker had to do something similar last week to read from a ESB. Think he used a Thread object and simply told it to poll every second inside a loop.

Edit: I'm sure there are better ways to do it, we were pulling a 24 hour day trying to get code up for a demo that was scheduled for 8 in the morning.

lamentable dustman fucked around with this message at 06:03 on Sep 24, 2008

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boz0r posted:

I'd like to be able to reverse an array of any primitive type.

Copy and paste, do some ridiculously inefficient things with reflection, or switch programming languages. Sorry.

RE negative bytes: Bytes being signed is one of the major flaws in the Java language specification, in my opinion. I've never heard a compelling argument for it, and every time I write any sort of encoder/decoder, I seem to have at least one signedness bug.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

pearofducks posted:

infinite loop

The preferred term for this is a "busy wait", and it's never a good idea. Ever. Some people will tell you about spin locks, and those people are crazy, treasonous, and wrong.

quote:

low thread priority

Making a thread low-priority just means it won't be scheduled as often if there are higher-priority runnable threads. If there aren't, it'll still happily use 100% of CPU.

quote:

Is there anything that will let me get away from this loop?

dvinnen's suggestion about Thread.sleep is not terrible, if you for some reason can't switch to a push architecture.

quote:

Maybe something that pushes the data instead of needing it to be constantly polled?

Yes, this is the right way to do it. Even if you have to use a pull protocol like HTTP for network communications, you should use push internally.

I don't know what your architecture is such that you're spinning on an input buffer, but the easiest way to write a client for this is to have a single thread take charge of blocking on the socket and parsing events from the input data; whenever it reads a new event, it acquires an appropriate lock, updates some data structures, and requests a repaint.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

rjmccall posted:

Copy and paste, do some ridiculously inefficient things with reflection, or switch programming languages. Sorry.

If you can use the Apache Commons Collection library, they have a method in CollectionUtils called reverseArray.

http://commons.apache.org/collections/index.html

Boz0r
Sep 7, 2006
The Rocketship in action.
I don't even know if this is still a Java question but here goes. I've gotten my map extractor working more or less, so this is fine tuning.
Some of the sectors are sloped, and they sloped around the first wall in the sector with a value of between -32768 and 32512, where 4096 is 45 degrees. I reckon that's something like tan(x)*4096.

I suck a trigonometry, so I've been wracking my brain as to how to find the offsets to different vertices. I've got the x and y coords for every point, plus the z coord for the first point.

I've also made this pretty picture detailing the problem. The floor is supposed rotated around the fat line.

If anyone understands my rambling and is able to help me make this in pseudo-code or java, I'll be very grateful.

Only registered members can see post attachments!

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boz0r posted:

I've also made this pretty picture detailing the problem. The floor is supposed rotated around the fat line.

It sounds like you're working in two dimensions, but how do you "rotate" an object in two dimensions around a line?

Boz0r
Sep 7, 2006
The Rocketship in action.

rjmccall posted:

It sounds like you're working in two dimensions, but how do you "rotate" an object in two dimensions around a line?

I don't really know how to word it best, but it's in 3D, and it's not really a rotation. It's more moving the vertices that aren't touching the fat line up or down the z-axis to making it an d-degree slope.

It has to look like this(+30 degree slope):

Only registered members can see post attachments!

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boz0r posted:

I don't really know how to word it best, but it's in 3D, and it's not really a rotation. It's more moving the vertices that aren't touching the fat line up or down the z-axis to making it an d-degree slope.

Okay, and you're assuming that all the points are in the same z-plane as the line before this transformation?

Boz0r
Sep 7, 2006
The Rocketship in action.

rjmccall posted:

Okay, and you're assuming that all the points are in the same z-plane as the line before this transformation?

Yes. This is supposedly the only operation that can change the z-coords of individual vertices in the Build engine.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Anyone have experience in image processing?

I wrote a servlet that resizes the image on the server side and sends the client a thumbnail of the original picture. Works really well and all.

We ran into a issue with a good portion of our pictures are corrupt. We haven't figured out were in the process it is corrupting yet as part of the process is out of our control. Now my servelet has nothing to do with the corruption but they want to see if we can have some kind of checker on it anyways, just for future issues.

Doing some googling right now and not seeing much. I'm also pretty sure it isn't possible without a hash to compare against but figured it is worth asking. Looking at files in a HEX editor dosen't show anything I might be about to compare against either.

The images are in jpeg format and I am turning them into a BufferedImage for processing.


EDIT: I was able to figure it out. The curropt images throws exceptions when you look at the metadata. Ran the code from the guy bellow and was getting "Bogus marker length" from javax.imageio.IIOException. Dude has a good blog, I've learned a lot of Perl tricks from him.

http://johnbokma.com/java/obtaining-image-metadata.html

lamentable dustman fucked around with this message at 16:10 on Sep 25, 2008

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Boz0r posted:

Yes. This is supposedly the only operation that can change the z-coords of individual vertices in the Build engine.

Okay. So it sounds like you want to calculate the signed distance of each point to the "line of rotation", then set its z-value to that distance multiplied by some scaling factor.

Boz0r
Sep 7, 2006
The Rocketship in action.

rjmccall posted:

Okay. So it sounds like you want to calculate the signed distance of each point to the "line of rotation", then set its z-value to that distance multiplied by some scaling factor.

Yes. I've made more pictures:

Only registered members can see post attachments!

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Computing the distance between a point and a line. In this case, you can precompute P2' such that |P2' - P1| = 1 and hence avoid the division. The easiest way to figure out which side it's on is with the signum of the cross-product of (P2-P1) and (P3-P1).

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Is there a way to iterate through every file in a directory? I've been looking at the File class and not seeing a way to do it. This has to do with my question yesterday, I want to clear out all the corrupt images that we are receiving.

They are stored on a Linux server, so Perl might be better suited for the task, it's just that I got a method to check for bad images already written in Java. Don't really want to try and reimplement it. That and Perl is yukky and I try to stay away from it when possible.


EDIT:

I'm an idiot. I missed the method listFiles(filter) in the File class. Something about posting a question here lets me figure out the answer in 5 minutes.

lamentable dustman fucked around with this message at 16:59 on Sep 26, 2008

pogothemonkey0
Oct 13, 2005

:shepface:God I fucking love Diablo 3 gold, it even paid for this shitty title:shepface:
This is difficult to explain, so bear with me.

I will be comparing two strings, each of varying length, and storing the comparison value to be looked up later. I need to know how to store these comparisons.

If I have already compared the strings "SH" and "SO," I need to look up the value in my data structure. I need a two dimensional structure where I can say, "I need the value at the index of SH and SO." I have no idea what structure can do that. An array wont work because the index is an int.

Any ideas?

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

pogothemonkey0 posted:

This is difficult to explain, so bear with me.

I will be comparing two strings, each of varying length, and storing the comparison value to be looked up later. I need to know how to store these comparisons.

If I have already compared the strings "SH" and "SO," I need to look up the value in my data structure. I need a two dimensional structure where I can say, "I need the value at the index of SH and SO." I have no idea what structure can do that. An array wont work because the index is an int.

Any ideas?

I'd personally just use a HashMap and depending on the amount of comparisons, I'd maybe make a Map<String, Map <String, Integer>> so a map that holds a map of strings to comparisons. That said, bookkeeping would be a pain in the rear end unless you know the order of string comparisons would remain the same.

pogothemonkey0
Oct 13, 2005

:shepface:God I fucking love Diablo 3 gold, it even paid for this shitty title:shepface:
That is exactly what I thought too, but it doesnt work well. After taking the time to implement, I realized that it wont work. Because you can only have one instance of a "key," I cant have different values associated with one string.

I need to be able to store Cat-dog as well as Cat-rat. Because there can be only one cat key, the old one is erased. I need to find a better way.

I just thought of this:
I can make a HashMap that contains the Strings and ints. If I increase the int every time I add to the HashMap, I will have a working index of the Strings inside of it. That way, I can use the int from the HashMap as an index to another Array full of HashMaps. The second HashMap will consist of the second strings as indexes and give the final int values.

The only problem for this is how convoluted it is and that I will need a separate static int counter for the index.

pogothemonkey0 fucked around with this message at 01:11 on Sep 28, 2008

LakeMalcom
Jul 3, 2000

It's raining on prom night.
just use a hashmap with the strings concatenated as your key? if order matters, then roll your own little map to put and get the reverse order string.

code:
Map<String, Object> map = new HashMap<String,Object>();
map.put(str1+str2, value);

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

LakeMalcom posted:

just use a hashmap with the strings concatenated as your key? if order matters, then roll your own little map to put and get the reverse order string.

code:
Map<String, Object> map = new HashMap<String,Object>();
map.put(str1+str2, value);

Concatenation is dangerous because you can end up with collisions.

What I was suggesting is a map system like this (in Groovy syntax cause I'm lazy):
code:
[cat:[rat:1, dog:2]] 
There's no reason 'cat' would be overwritten nor should any data be lost. You have access to all of cat's comparisons. So I guess I don't see the issue... unless I don't understand what you're saying.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

LakeMalcom posted:

If order matters, then roll your own little map to put and get the reverse order string.

The TreeMap lets you take ordering into account.

LakeMalcom
Jul 3, 2000

It's raining on prom night.

pogothemonkey0 posted:

I will be comparing two strings, each of varying length, and storing the comparison value to be looked up later

TRex EaterofCars posted:

Concatenation is dangerous because you can end up with collisions.
Maybe I'm reading this wrong, but it sounds like collisions aren't a problem here. If the value is generated from some property of the strings, then the same values will be produced if the strings are operated on again. Hell, he can memoize the algorithm this way by checking for null first.

pogothemonkey0
Oct 13, 2005

:shepface:God I fucking love Diablo 3 gold, it even paid for this shitty title:shepface:
Yeah TRex, I had the same idea to begin with and my last post stated that there were problems. The problem was actually in my implementation of it, not in the setup itself.

To add things to a HashMap, you need to use HashMap.put(key, value). When I added new information to the map, I was just overwriting the old information because I didnt think when I coded it.

If I had:
[cat:[rat:1, dog:2]]

And I added [bat:5] to the cat key, I was doing HashMap.put(cat, [bat:5]), overwriting what was already there.
To fix it, I needed to access the HashMap that was already in place and add to it:

HashMap.get(cat).put([bat:5])

(yeah the brackets arent really java, but it is a lot easier to write that way)

And to the above poster, this is a memoization project.

The overarching structure is HashMap<String, HashMap<String, Integer>>

pogothemonkey0 fucked around with this message at 07:32 on Sep 28, 2008

Incoherence
May 22, 2004

POYO AND TEAR

pogothemonkey0 posted:

That is exactly what I thought too, but it doesnt work well. After taking the time to implement, I realized that it wont work. Because you can only have one instance of a "key," I cant have different values associated with one string.

I need to be able to store Cat-dog as well as Cat-rat. Because there can be only one cat key, the old one is erased. I need to find a better way.
Unless I'm misunderstanding your problem, what you want is to make some sort of pair structure (not concatenating, unless you can concatenate them with some special character between them that's not going to be in your input). Make a Pair object with your two strings (I don't think there's a builtin class for this but it shouldn't be hard to write), declare a HashMap<Pair, Integer>, add to it with put(new Pair(firstString, secondString), value), and then everything's happy. Only problem with this is that you end up making a lot of temporary Pair objects.

LakeMalcom
Jul 3, 2000

It's raining on prom night.
This is a good idea, but you would have to implement equals() and hashCode() for the new Pair class. (Not that this makes it any less of a good idea, just something to take into account.)

I think the HashMap<String, HashMap<String, Integer>> is fine, though. Certainly gets the job done.

pogothemonkey0
Oct 13, 2005

:shepface:God I fucking love Diablo 3 gold, it even paid for this shitty title:shepface:
Yeah, it works as intended for the most part. There is some problem with my code that I havent found yet, but the data structure works fine.

Thanks for the input.

Adbot
ADBOT LOVES YOU

pearofducks
Jan 21, 2003

quack?
Quick concurrency question (I'm new to threads). I've got a homemade generic linked list, most of the operations on it take place inside a loop, and only one operation actually uses the list. So I'm wondering if its possible to have the linked list operation run in its own thread, and the loop to keep going. I've read up on it some and it seems like it just provides the ability to override the run method, but I'd like to have any of the linked list actions be in their own thread. Is this possible? If so can someone just give me a brief idea of what to do?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply