Browsing 7239 questions and answers with Jon Skeet

How can I get a hash of the body of a method in .net?

I'd like to generate a hash of a method body or entire class in order to compare it to a previous hash and therefore detect changes at run-time. I've seen reference to this being...
Jon Skeet
people
quotationmark

You can use MethodInfo.GetMethodBody() to get a MethodBody, then create a hash based on whatever you like (e.g. the IL byte array) using whatever kind of hash you want (e.g. SHA-256). Of course, it's possible for the IL to change without... more 1/23/2015 11:07:38 AM

people

How to copy files in JAVA by bufferedInputStream and bufferedOutputStream?

I would like to use bufferedInputStream and bufferedOutputStream to copy large binary files from source file to destination file. Here is my code: byte[] buffer = new...
Jon Skeet
people
quotationmark

This is the mistake: bos.write(buffer); You're writing out the whole buffer, even if you only read data into part of it. You should use: bos.write(buffer, 0, numBytes); I'd also suggest using try-with-resources if you're using Java 7... more 1/23/2015 10:27:46 AM

people

Regex.Split ignores empty results

I have this string: IMD+F++:::PS4 SAINTS R IV R?+GA' I would like to split it up in two steps. First I would like to split on + except escaped plusses "?+". Second I want to...
Jon Skeet
people
quotationmark

You're explicitly saying that you want to split on "at least one plus" - that's what [\+]+ means. That's why it's treating ++ as a single separator. Just split on a single plus - and note that you don't need to put that into a set of... more 1/23/2015 7:32:11 AM

people

Facing issue with Calendar object

I have a task in which i need to set hour, minute, meridian programmatically to Calendar object and need to display time in a format hh:mm a. Here below is my code so...
Jon Skeet
people
quotationmark

The HOUR field is documented as: Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). So instead of setting it to 12, you should set it to 0. Personally I'd just... more 1/23/2015 7:09:23 AM

people

JodaTime Setting TimeZone over Existing DateTime

I would like to set a timezone over a constant DateTime. I want to create a single DateTime in a certain timezone. Set a new timezone to get a calculated DateTime object....
Jon Skeet
people
quotationmark

Assuming you want "the same instant, just in a different time zone" then you want withZone. It's not setZone, because DateTime (like many types in Joda Time) is immutable. Instead, you use withZone and use the result. For... more 1/22/2015 8:50:37 PM

people

JAVA: Different objects with different methods

I want different objects to have different methods For example how to make following java pseudocode work? Button b[] = new Button[5]; button[0].onClick = new...
Jon Skeet
people
quotationmark

You can't do that exactly, but you could have: button.addClickHandler(clickHandler); where clickHandler could actually be a lambda expression implementing some ClickHandler interface. The Button class itself would be responsible for... more 1/22/2015 8:06:44 PM

people

Single field extraction from a database using lambda

My problem is connected to the extraction method called lambda ( I am using it in C# ). I have a database and in it I have a table called Students and I wonder if I would be able...
Jon Skeet
people
quotationmark

Currently you're calling Select on an IQueryable<string> - your query represents a sequence of student first names, even if it only matches a single one. If you only want a single name, you can use: var studentName = ... more 1/22/2015 6:07:49 PM

people

Fun with generics: no implicit reference conversion error

I tested this code and got that it doesn't compiles. interface IE<T> { } class A<T> : IE<T> { public static void F<TU>() where TU : IE<T> ...
Jon Skeet
people
quotationmark

No, it's not valid. The constraint of where TU : IE<T> refers to the current T, i.e. the one for the type on which you're calling this method. Consider a call of: A<string>.Foo(); That's trying to pass A<int> as a... more 1/22/2015 4:08:34 PM

people

DB2 end of stream prematurely reached while reading inputstream

Following code: InputStream imageData=image.getInputStream(); byte[] bytes = ByteStreams.toByteArray(imageData); pst.setBinaryStream(3, imageData, (int)bytes.length); gives...
Jon Skeet
people
quotationmark

I suspect the problem is that you've already read the data by this point in order to obtain bytes... so when the driver tries to read from imageData, it immediately finds there's no more data to read. I suggest you just set the stream... more 1/22/2015 3:51:24 PM

people

Is code cleanliness a good enough reason to use a property over a field?

I have a class that is Enumerated to allow subscripting of a list contained within it. For this reason, the List does not need to be publicly accessible through a field or...
Jon Skeet
people
quotationmark

I wouldn't introduce a fully-private property just for the sake of it. It can be useful to do so if it computes the property value from state, or performs validation etc - but don't just do it for the sake of it. Personally I'd just call... more 1/22/2015 3:07:56 PM

people