Browsing 15528 questions and answers with Jon Skeet

How to copy image in java using bufferedreader/writer

File file = new File("download.png"); File newfile = new File("D:\\Java.png"); BufferedReader br=null; BufferedWriter bw=null; try { FileReader fr =...
Jon Skeet
people
quotationmark

Whats wrong with this code. You're using text-based classes for binary data. Is it possible with BufferedReader and Writer Class? Not while you're dealing with binary data, no. I know how to to make copy of image using... more 2/16/2017 3:57:55 PM

people

why does i.Parent.ReplaceWith(i) not throw an exception?

In these statements (running with MoreLinq): var xml = @" <div> <p> <h2>hey</h2> </p> <pre /> <h2 class=""cool"" /> <p> ...
Jon Skeet
people
quotationmark

You don't need MoreLINQ to demonstrate this at all - and you can simplify the sample code, too: using System; using System.Linq; using System.Xml.Linq; class Program { static void Main() { var element = new XElement( ... more 2/16/2017 9:15:54 AM

people

C# convert System.Func<Tderived, bool> to System/Func<Tbase, bool>

I've googled but couldn't find satisfactory answers. I'm basically trying to get this code to work: public List<WordEntry> WordDataBase = new...
Jon Skeet
people
quotationmark

Basically, you just need to cast - the language rules don't allow the compiler to take the if statement into account when it thinks about the types involved. Note that you also need to call ToList<Entry>(), specifying the type... more 2/15/2017 3:18:47 PM

people

java reading 2 byte integer in 2's complement from binary file into integer

I am trying to parse a binary file containing values. In the specs it says that each value is represented as a 2-byte integer in 2's complement format. I am reading the file into...
Jon Skeet
people
quotationmark

Yes - you're reading them in little-endian format (the first value is the least important) but you're writing them in big-endian format (the first value is the most important), assuming you're writing a then b. Just reverse the order when... more 2/14/2017 10:13:16 AM

people

Select method is not called using LinQ query syntax

I want to enable LinQ query syntax over my classes. I think query syntax is translated to method syntax, for example: var query = from p in new Class1<Product>() ...
Jon Skeet
people
quotationmark

Someone knows why? Yes, because that's what the language specification says to do. The query expression translation is all in section 7.16.2 of the C# 5 specification. Section 7.16.2.5 explains why your initial example is incorrect... more 2/13/2017 12:48:00 PM

people

C# func<> applied to the object on left

I'm trying to figure out if there's a way to use func (or a similar method) applied to the left object. Ex.: member1.MyFunction() Here's a simplified context of usage. We got...
Jon Skeet
people
quotationmark

Well not with quite that syntax, but you can just use: theSum += aFunction(aList[i]); I suppose you could write an extension method for this, but the above is more idiomatic. In fact, using LINQ to start with would be more idiomatic -... more 2/12/2017 7:34:36 PM

people

How to read byte array as a property from a Properties file

Suppose I have a properties file named features.properties with some of its properties encrypted in byte array format. e.g. color = [64, 14, 76, 92, 5, 114, 54, 31] I want...
Jon Skeet
people
quotationmark

with some of its properties encrypted in byte array format I suspect you don't actually mean "encrypted" here. You're just representing the bytes as text - I don't see any encryption. If they really are encrypted as well, that's a... more 2/11/2017 8:46:04 AM

people

Java replaceAll fails with dollar sign in source string

Say I have the following code String test = "$abc<>"; test = test.replaceAll("[^A-Za-z0-9./,#-' ]", ""); test is now "$abc". Why does it keep the dollar sign?
Jon Skeet
people
quotationmark

Your list of characters to preserve includes #-', which is a range from Unicode U+0023 (the # symbol) to U+0027 (the ' symbol), including $ (U+0024). If you meant #-' to be interpreted as a list of three characters, just escape it: test... more 2/10/2017 8:17:18 PM

people

What's the purpose of the Newtonsoft.Json.JsonToken.StartConstructor token?

The JsonToken enum in Newtonsoft.Json namespace contains, among others, the entry JsonToken.StartConstructor. I most likely misinterpret the meaning of this token, as I take it...
Jon Skeet
people
quotationmark

Looking at the ParseTests source code, it looks like it's so that you can have JSON like this: { "date": new Date(2017, 2, 10) } ... which isn't actually valid JSON, but may be common in the wild. more 2/10/2017 11:36:55 AM

people

A URI scheme name 'pack' already has a registered custom parser

I am newborn to NUNIT test,facing error as 'A URI scheme name 'pack' already has a registered custom parser'. [SetUp] public void OnTestInitialize() { UriParser.Register(new...
Jon Skeet
people
quotationmark

Well presumably OnTestInitialize is currently being called once per test, but you want UriParser.Register to be called once in total. That's the sort of thing that makes sense to do in a static initializer, which is guaranteed to be run... more 2/10/2017 7:11:49 AM

people