Browsing 7239 questions and answers with Jon Skeet

Extract String from ReadOnly java.nio.ByteBuffer

How do you extract a String from a read-only ByteBuffer? I can't use the ByteBuffer.array() method because it throws a ReadOnlyException. Do I have to use ByteBuffer.get(arr[])...
Jon Skeet
people
quotationmark

You should be able to use Charset.decode(ByteBuffer) which will convert a ByteBuffer to a CharBuffer. Then just call toString() on that. Sample code: import java.nio.*; import java.nio.charset.*; class Test { public static void... more 5/8/2014 8:44:10 PM

people

Why I have this error when I try to dispose a resource in the finally statment of a try block?

I have some problem disposing a resource. I heve this code: class ChartHelper { //public static System.Drawing.Image GetPdfChart(int percentage) public...
Jon Skeet
people
quotationmark

Why? Because bitmap isn't definitely assigned, according to the rules of the language specification. Imagine if the Bitmap constructor threw an exception. What would you dispose? What can I do to solve it? (I don't want to use the... more 5/8/2014 5:45:35 PM

people

XElement with colon in the name doesn't work

i'm trying to make something like : new XElement("media:thumbnail", new XAttribute("width", "")) but i doesn't work, and i got a error because of the colon ':'. does anyone...
Jon Skeet
people
quotationmark

That's not how you create an XName with a namespace. You should create an XNamespace with the right URI, and then you can create the right XName easily - personally I use the + operator. So: XNamespace media = "... some URI here... more 5/8/2014 4:29:04 PM

people

Using items from a list of base class to generic method

Hard to come up with a good title, but let's say I have this situation. I have a base class: public abstract class FooBase { } And I have a generic implementation: public...
Jon Skeet
people
quotationmark

You can use dynamic typing - that will perform overload resolution at execution time with the actual type of the object that item refers to. You can simply change the iterator type to dynamic like this: public void... more 5/8/2014 2:40:53 PM

people

Strange behaviour of ww SimpleDateFormat

Can anyone explain why do I get those values while trying to parse a date? I've tried three different inputs, as follows: 1) Third week of 2013 Date date = new...
Jon Skeet
people
quotationmark

You're using ww, which is "week of week-year", but then yyyy which is "calendar year" rather than "week year". Setting the week-of-week-year and then setting the calendar year is a recipe for problems, because they're just separate... more 5/8/2014 12:50:23 PM

people

What does "Primitive values do not share state with other primitive values" mean?

Section 4.2 of the Java Language Specification states that, "Primitive values do not share state with other primitive values". What exactly does this mean?
Jon Skeet
people
quotationmark

I suspect it's drawing a distinction between primitives and reference types - where in the latter case, two values (references) can both refer to the same object. If you have two primitive variables, there is nothing you can do to one that... more 5/8/2014 2:16:27 AM

people

How many String objects will be created in String s="abc"+"xyz"; in prior versions of Java 1.5?

As per this link , in java versions 1.5 onward in code String s="abc"+"xyz"; only one object is created due to compiler optimization using StringBuilder class. new...
Jon Skeet
people
quotationmark

No, as far as I'm aware that has always been treated as a compile-time constant, and has always been equivalent to String s = "abcxyz"; Note that Java 1.5 introduced StringBuilder; before that execution-time string concatenation used... more 5/7/2014 7:36:24 PM

people

How does String invoke constructors?

The first code is shown below: String s = "hello"; The second code is shown below: String s = new String("hello"); Question: Are the two codes invoking the same constructor...
Jon Skeet
people
quotationmark

Question: Are the two codes invoking the same constructor of String(char[])? No, absolutely not. The first form bakes the the string content directly into the bytecode, then uses the ldc (load constant) instruction to load it from... more 5/7/2014 7:28:52 PM

people

C# mutex in release mode behaves different than in debug mode

So I have the following code: ... private static void Main(string[] args) { string file=DateTime.Now.ToFileTime().ToString(); File.AppendAllText(file, "Mutex\r\n"); ...
Jon Skeet
people
quotationmark

Along with Juan's answer, there's a difference in terms of garbage collection between launching inside and outside a debugger. That's not quite the same as a Debug configuration vs a Release configuration, but it's something you should be... more 5/7/2014 6:32:53 PM

people

LINQ query throws an InvalidCastException?

I was roaming around in different sites for casting techniques and I constructed the following code to cast to from float to int as shown below var floatList = new float[] {...
Jon Skeet
people
quotationmark

A LINQ clause of the form: from X x in y is equivalent to y.Cast<X>() and then using x as the range variable later. The rest of your query is degenerate, so your code is equivalent to: var intList =... more 5/7/2014 9:30:54 AM

people