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
Max Facetime
Apr 18, 2009

You could create your own InputStream class that wraps the underlying socket stream, then just ignore the close-method call that the parser makes. This is actually not as bad of a hack as it sounds; you would be treating a part of the underlying stream as a stand-alone stream.

Adbot
ADBOT LOVES YOU

Max Facetime
Apr 18, 2009

almostkorean posted:

code:
im = connectComponents(width,height,uf);
When the Java Virtual Machine is executing that code, there are two data structures in play:

The one called the operand stack holds temporary values. These are pushed on top of the stack by copying a value from somewhere and removed from the stack by popping a value, giving it to something else.

The other one called a stack frame holds values that were used to invoke the current method and the values of any local variables. This is a 0-based array.

In the simple case, where your code resides in a class method and width, height and im are arguments, the method call goes like this:
  1. the value in stack frame[0] holding the value of width is pushed on operand stack.
  2. the value in stack frame[1], aka height is pushed on operand stack.
  3. the reference value in stack frame[2], aka uf is pushed as well.
  4. the method connectComponents is invoked by getting a new stack frame, popping the three values from the operand stack and storing them in new the new stack frame in positions 2, 1, 0 respectively, executing the method and finally pushing the return value to the operand stack.
  5. then, the reference value on the operand stack is popped and stored in stack frame[2], aka im.

Max Facetime
Apr 18, 2009

My first guess would be that the TestClass.class file is in the root of the TestClass.jar and not in org/me/extension.

If that's not the case, I'd start debugging by using a JarInputStream to make sure the .jar is good. Maybe compare to a known good .jar.

Max Facetime
Apr 18, 2009

It's most likely your code. For example, ArrayDeque.clear() is not likely to reduce the capacity of your aHugeFuckinArrayDeque, so if you're holding a reference to the objects somewhere else, calling it is not going to make any memory eligible for garbage-collection.

Max Facetime
Apr 18, 2009

That HTTP response looks a bit confused with that extra error document tacked on at the end. I'd make double sure that there are no subtle differences with the headers of the working and non-working requests. You can use Wireshark to listen in on the request Java is making. That should tell you which request headers you should tweak in Java to make it work.

Max Facetime
Apr 18, 2009

As in the OP's case the invalid URL is being used internally by Java, instead of switching to another HTTP library another option would be to use HttpURLConnection and handling the redirection manually. It's pretty easy:

1) Create the connection: (HttpURLConnection) new URL("http://somepath").openConnection() is guaranteed to work.
2) Disable redirection: .setInstanceFollowRedirects(false)
3) Check the response code: .getResponseCode()
4) Get and fix the real location: .getHeaderField("Location").replace(' ', '+')
5) Make a normal connection with the fixed URL.

Max Facetime
Apr 18, 2009

Use Collections.unmodifiableSet(set).

Max Facetime
Apr 18, 2009

tohveli posted:

Well the Set thing was just an example. I have my own classes I pass, how do I make them unmodifiable?

The same principle applies. You can wrap it in an unrelated class that provides a read-only view.

Max Facetime
Apr 18, 2009

fletcher posted:

Any suggestions?

Maybe it doesn't like a username that has a number in a wrong place.

Generally, to narrow it down you'll want to do a binary search on your rows, i.e add rows 1-5000 or rows 5001-10000, then depending on which one is invalid split that range in two until you have the offending row.

Max Facetime
Apr 18, 2009

Knowing the line that throws the NPE would help, but you aren't checking against null in your second example, so I'd guess that's the culprit.

E: The line numbers are printed in the exception stacktrace.

Max Facetime fucked around with this message at 08:26 on Sep 24, 2009

Max Facetime
Apr 18, 2009

Short answer: when getting a byte representation of a String for transmission, you want to specify the encoding to use, in this case ASCII or UTF-8.

Longer answer: Java Strings are stored in memory as a 2-byte Unicode variant codepoints, and the deal with data stream classes is that they closely mimic the memory storage. So those zeros are what's in memory.

e: also, DataOutputStream writes the lengths of Strings so that a DataInputStream could read them back in, so you probably shouldn't use it unless there's a corresponding class reading it back in.

Max Facetime fucked around with this message at 19:58 on Oct 19, 2009

Max Facetime
Apr 18, 2009

dis astranagant posted:

Ugh, and I was told that DataOutputStream was the only way to send text and binaries over the same socket. JAVA :argh:

It's a good way if you're transmitting between Java programs and don't mind using your own protocol.

But interfacing with the rest of the world, it's better to use a dedicated library or low-level calls like

String.getBytes(String charsetName) and
OutputStream.write(byte[] bytes, int off, int len)

Max Facetime
Apr 18, 2009

geeves posted:

Any thoughts? Am I blatantly missing something?

I don't see relatedLinks being initialized, so it's probably null. Another thing is that arrays in Java do not expand after creation.

Taken together, relatedLinks should be

List<String> relatedLinks = new ArrayList<String>();

Max Facetime
Apr 18, 2009

yatagan posted:

Do you think it's bad form to do this?

Referring in particular to the call to toArray().

No, Strings are immutable and while arrays are not, the responsibility for the array contents is clearly passed to the called method.

Max Facetime
Apr 18, 2009

fletcher posted:

What's a good way of figuring out the difference between two calendar objects and offsetting another calendar object by that amount?

Right now I'm using Calendar.getTimeInMillis(), which returns a long, and then just casting it to an int and using Calendar.add(). There's got to be a better way than that. Maybe using a loop and increment it one day at a time and just count the number of iterations to get to the other calendar?

Well, the simple and straightforward way is

code:
long time1 = cal1.getTimeInMillis();
long time2 = cal2.getTimeInMillis();
long difference = time2 - time1;
long time3 = cal3.getTimeInMillis();
time3 += difference;
cal3.setTimeInMillis(time3);
Things get more complicated if the difference is not simply a difference in milliseconds. It could also be a difference in full days, full months, full months+days or something else. Are leap days going to affect things? Or daylight saving time?

Max Facetime
Apr 18, 2009

PT6A posted:

Is there an easy way to create a Swing GUI (or part thereof) with overlapping components, but with as little absolute positioning as possible and greatest use of existing Swing widgets? Specifically, I'm working on a simple card game, and I want to have cards overlap nicely when displaying hands.

I haven't used Swing much, but that doesn't sound like something Swing is intended for. Maybe use JavaFX for drawing the card game and Swing around it for GUI?

Max Facetime
Apr 18, 2009

LakeMalcom posted:

For the random number/long guy: how about this: http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#BigInteger(int,%20java.util.Random)

Do this, but use it to implement nextInt(int n) from earlier using BigIntegers. Using modulo to limit the random number range is a really bad idea. In short, if you have range and distribution

----
[0-3]

doing a % 3 gives you:

-
---
[0-2]

so number 0 is now twice as likely.

Max Facetime
Apr 18, 2009

Also http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#bitLength%28%29

Max Facetime
Apr 18, 2009

I haven't used HQL that much (I'm more either Criteria or native SQL kinda guy), but a little googling produced this.

Basically your query should be "... where :column like :value" and have the %-characters in the parameter value itself. It probably has to do with how HQL is parsed.

Max Facetime
Apr 18, 2009

wigga please posted:

code:
from Actor a where ? like ?
ignoring both the single quotes and percent signs.

That looks like parameterised SQL. Having a parameterised column name like that is probably not supported on your database out of the box. Try it by hard-coding the column name before handing it over to Hibernate.

Max Facetime
Apr 18, 2009

I guess you could read directly from the console input by doing System.setIn(yourInputStream);

Max Facetime
Apr 18, 2009

Also, the solution 27^5 + 84^5 + 110^5 + 133^5 = 144^5 is a hint to the algorithm.

Max Facetime
Apr 18, 2009

Your hasNext() has two ways to check if there are more nodes after the current. You should choose one way and stick with that. How is hasNext() going to work on an empty list?

Max Facetime
Apr 18, 2009

epswing posted:

code:
    public boolean hasNext() {
        boolean nextIsNull = curr.getNext() == null,
                dataIsNull = curr.getData() == null;
        return (nextIsNull || dataIsNull);
    }
...but even that is pushing it.

Yeah, don't do this mechanically. nextIsNull doesn't tell anything more than curr.getNext() == null. In this context, you could name them

     boolean isLastNode = curr.getNext() == null;
     boolean isFirstOrLastNode = curr.getData() == null;

which gives

     return isFirstOrLastNode || isLastNode;

which suggests there is a design problem.

Max Facetime
Apr 18, 2009

not a dinosaur posted:

I have a list of integer message types that I want to map to strings. They will never change at runtime. I know about HashMap, but where is the idiomatic Java place to initialize it with all my poo poo

Put it all in a text file, then access it using a ResourceBundle. No initialization required.

If you really want to do it your way, then a static initializer block sounds about right.

Max Facetime fucked around with this message at 17:25 on Mar 10, 2010

Max Facetime
Apr 18, 2009

ToastedZergling posted:

tldr; I prefer leveraging existing frameworks than writing custom parsing

I doubt there's such a thing, here are a couple of reasons:

Yahoo! Mail: The best web-based email!

.NET Rocks! - My .NET Story

E*TRADE del.icio.us Mercedes-Benz

Fake edit: good luck :)

Max Facetime
Apr 18, 2009

fletcher posted:

Bump! This is driving me nuts!

I looked at the cookie that is sent with Firebug. These keys have values: sessionid, __utma, aduserid, bbuserid, bbpassword and sessionhash.

Try to catch them all from your browser and see if it works. It's hard to say why it works the first time without knowing the internals of the forums software.

Max Facetime
Apr 18, 2009

fletcher posted:

Is there a way to check and see if anything has been written to a HttpServletResponse?

isCommintted() may be what you're looking for in ServletResponse.

Max Facetime
Apr 18, 2009

You could wrap the original HttpServletResponse like this:

code:
class MyResponse extends HttpServletResponseWrapper
then return a similarly wrapped OutputStream from getOutputStream(), but consider do you really want to do this instead of catching an exception or an error code? The thing about isCommitted() is that you can rewind a partially rendered response and render an error page instead.

E: beaten!

Max Facetime
Apr 18, 2009

Is there a way to escape Expression Language results in a JSP file by default?

I have ${item.value} in my JSP file, which generates the following code with Tomcat 6.0:

code:
out.write((java.lang.String) 
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate
("${item.value}", java.lang.String.class, 
(PageContext)_jspx_page_context, null, false));
I would like that to be something like:
code:
out.write(somepackage.SomeClass.escape((java.lang.String) 
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate
("${item.value}", java.lang.String.class, 
(PageContext)_jspx_page_context, null, false)));

Max Facetime
Apr 18, 2009

Yes. I could use c:out or roll my own, but I'd rather use

${item.value} for escaped text and

<c:out value="${item.value}" escapeXml="false"/> for unescaped text.

I've looked at replacing the Jasper compiler in Tomcat with a modified version, but I'd like to find an easier way.

Max Facetime
Apr 18, 2009

I'll give a hint about the culprit. If you redo the code above to use this brace style
code:
if(condition) {
  doSomething();
}
you should see the problem quite quickly.

Max Facetime
Apr 18, 2009

Flobbster posted:

Telling him to switch to an inferior brace style is a terrible way of pointing him to his problem.

Mmmmmm, just try it on for a couple of weeks, see if it fits. You can always go back real easily.

Max Facetime
Apr 18, 2009

korofrog posted:

I've tried that style of braces and I personally don't like it. I like seeing my braces line up.

My comment wasn't really about whether one brace style is better than another, rather it was the simplest step that would likely help you see the problem yourself.

Max Facetime
Apr 18, 2009

When compiling the first version my compiler stores null to the local variable even though the null value can never be read, resulting in a few bytes bigger method. Therefore the second version is more space-efficient.

If I remove the null initialization, both versions compile to identical byte-code.

If the method is called often enough to be JIT compiled, anything could happen.

Max Facetime
Apr 18, 2009

finally-blocks are meant for cleaning up resources. The trouble with streams is that the close-method is also responsible for committing changes made to the stream. In this case the GZIPOutputStream is probably doing some internal buffering, so the underlying ByteArrayOutputStream receives the data too late.

To make sure this doesn't happen I usually structure my stream handling like this:

code:
String process() throws IOException {
  InputStream in = null;
  try {
    in = acquireStream();
    String result = readStream(in);
    in.close();
    in = null;

    return result;

  } finally {
    close(in);
  }
}

void close(InputStream in) {
  if(in != null) {
    try {
      in.close();
    } catch(IOException ignored) {}
  }
}
There's two places where the stream is closed: first one commits changes, releases resources and can signal an error and the second just cleans up when an error has already happened.

Max Facetime
Apr 18, 2009

Maybe it would work using a local workspace on the Windows machine, then Import...->Existing Projects into Workspace, point it to \My Dropbox and then tell it to not copy the project files to the workspace?

I don't think putting the workspace itself to Dropbox is a good idea.

Max Facetime
Apr 18, 2009

Magicmat posted:

You can see my horrible, messy, prototype-quality code here.

You're not closing the connections or streams properly, so they might be closed only when the program terminates. This could affect the order and timestamps of the log entries the webserver outputs.

Also, try adding a 10 second delay between testing different proxies to see whether the duplicate log entries come from the same or different proxies.

Max Facetime
Apr 18, 2009

OddObserver posted:

Any chance for a link on that? Can't seem to find it on the Sun^wOracle website.

It's called Project Coin. The proposal looks like to add a list of suppressed exceptions to Throwable, which deals neatly with the case where closing a resource throws an exception while another exception has already been thrown.

Adbot
ADBOT LOVES YOU

Max Facetime
Apr 18, 2009

BigInteger has also a remainder method, which is defined in terms of the %-operator.

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