Browsing 7239 questions and answers with Jon Skeet

base64 url safe removes =

The following code(using commons codec Base64): byte[] a = Hex.decodeHex("9349c513ed080dab".toCharArray()); System.out.println(Base64.encodeBase64URLSafeString(a)); ...
Jon Skeet
people
quotationmark

Why is this happening? Because that's what it's documented to do: Note: no padding is added. The = characters at the end of a base64 string are called padding. They're used to make sure that the final string's length is a... more 12/23/2017 7:21:30 PM

people

LINQ query syntax with multiple statements

Can this method be rewritten using LINQ's query syntax? public IEnumerable<Item> GetAllItems() { return Tabs.SelectMany(tab => { tab.Pick(); ...
Jon Skeet
people
quotationmark

No, query expressions in LINQ require each selection part etc to be a single expression, not multiple statements. However, you could write a separate method: public IEnumerable<Item> PickItems(Tab tab) { tab.Pick(); return... more 12/23/2017 9:42:02 AM

people

Error when using the WHERE clause in Linq to SQL

I have a datacontext that I'm trying to query, the results of which I want to bind to a gridview on a button click. Getting connected to the datacontext works great. I get the...
Jon Skeet
people
quotationmark

LINQ to SQL doesn't know what to do when you try to access the session inside the query. Instead of doing that, fetch the value from the session before the query and store the result in a local variable, then use that local variable in... more 12/18/2017 6:21:56 PM

people

MethodHandle invokeExact a static method with return and parameter

import java.lang.invoke.*; public class InvokeDynamicDemo { public static double doubleIt(double d){ System.out.print("Doubling it"); return d*2; ...
Jon Skeet
people
quotationmark

The problem is that you're not using the result of the invokeExact method. I hadn't seen this method before, but it looks like the Java compiler has to handle it in a very special way. From the MethodHandle documentation: As is usual... more 12/16/2017 7:48:42 AM

people

Downloading from google cloud storage always incorrect hash

I'm trying to download some files from google cloud storage (log files from a google play published app). My code looks like...
Jon Skeet
people
quotationmark

TL;DR: Update to 2.1.0 when that's out. (Or fetch and build the source before then if you're desperate.) This was a tricky one to fix. The issue is that HttpClient was automatically decompressing the data on the fly, but the hash... more 12/15/2017 11:46:07 AM

people

Thread safe Singleton: why the memory model does not guarantee that the new instance will be seen by other threads?

I have read in Jon's Skeet online page about how to create a thread safe Singleton in C# http://csharpindepth.com/Articles/General/Singleton.aspx // Bad code! Do not use! public...
Jon Skeet
people
quotationmark

Can you please explain why doesn't the memory model does not guarantee that the new value of instance will be seen by other threads? The memory model is complex and not terribly clearly documented at the moment, but fundamentally... more 12/14/2017 3:29:15 PM

people

Avoid copying compressed data when using DeflateStream

Assume we have given an API function f(Stream s) to put binary data contained in a stream into a database. I want to put a file into the database using f but I want to compress...
Jon Skeet
people
quotationmark

You can use SharpCompress for this. Its DeflateStream allows you to read the compressed data on the fly, which is exactly what you want. Here's a complete example based on Sir Rufo's: using System; using System.IO; using... more 12/14/2017 12:29:57 PM

people

Datetime parsing error

I am having problem parsing dates in Java. Below is the code. String dateString = "2017-12-13T16:49:20.730555904Z"; List<String> formatStrings =...
Jon Skeet
people
quotationmark

S in SimpleDateFormat specifies a number of milliseconds, not a fraction of a second. You've specified 730555904 milliseconds, which is ~8.45 days - hence the date change. java.util.Date only has a precision of milliseconds. I would... more 12/13/2017 5:20:26 PM

people

How to access objects outside of a lambda inside forEach?

I have the following code: String fields = ""; listofObjects.stream().forEach(l -> fields = fields + l.text); Which doesn't work, because fields should be final. How can I...
Jon Skeet
people
quotationmark

The smallest change to make that work would just be to use a StringBuilder instead - and that would be more efficient: StringBuilder builder = new StringBuilder(); listofObjects.stream().forEach(l -> builder.append(l.text)); String... more 12/13/2017 7:37:03 AM

people

javascript new Date with string param has wrong date

I am creating a new date from string var s = "2017-12-06" var dt = new Date(s) console.log(dt) // outputs Tue Dec 05 2017 19:00:00 GMT-0500 (EST) What am I missing ?
Jon Skeet
people
quotationmark

Date.toString() is formatted in your local time zone, but because you've passed in an ISO-8601 string, the value is parsed as if it's UTC. From the Date.parse() documentation (as the Date(String) constructor is documented to behave like... more 12/11/2017 2:20:01 PM

people