Browsing 15980 questions and answers with Jon Skeet

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

Confusion as to initialization (or lack thereof) of object

package lab9; import java.util.Scanner; import java.util.Date; /** * Write a description of class AccountManager here. * * @author (your name) * @version (a version number...
Jon Skeet
people
quotationmark

This is the problem: AccountManager accountmanager= new AccountManager (account, t, accountmanager); You're declaring a variable, and trying to read from that variable in the same statement that gives it an initial value. Here's a... more 12/8/2017 4:49:56 PM

people

Noda Timezone issue with IDateTimeZoneSource.MapTimeZoneId

I am trying to convert Windows to IANA time zone and below is the code that works most of the time. But when the windows time id = "Turks & Caicos" then below code returns...
Jon Skeet
people
quotationmark

GetSystemDefaultId doesn't replace all the functionality of MapTimeZoneId - you don't want to call that here. Instead, use tzdbSource.WindowsMapping to get a WindowsZones that you can then use to find the TZDB ID. Here's some sample... more 12/8/2017 4:21:36 PM

people

C# Add 1 day in specific TimeZone to DateTimeOffset

I have an instance of DateTimeOffset and I need to add 1 day to it in specific TimeZone (W. Europe Standard Time) taking into account daylight saving rules (so it might result in...
Jon Skeet
people
quotationmark

TimeZoneInfo makes this reasonably simple - just add a day to the DateTime part of the value, check whether the result is skipped or ambiguous, and if not, ask the zone for the UTC offset. Here's a complete example showing all the... more 12/8/2017 9:46:26 AM

people