BALTIMORE — A former spy agency official accused of leaking classified information to a newspaper walked out of court a free man on Friday, sentenced to a year’s probation and community service, after hearing the judge excoriate the government for its handling of the case.

Judge Richard D. Bennett of the Federal District Court praised the former National Security Agency official, Thomas A. Drake, for his exemplary record of public service before giving him a mild scolding for improperly providing information on alleged agency mismanagement to The Baltimore Sun.

But Judge Bennett reserved his strongest condemnation for the Justice Department, saying the two and a half years that elapsed between the search of Mr. Drake’s home and his indictment in 2010 was far too long.

The visibly angry judge said that Mr. Drake had been through “four years of hell” and that the dragging out of the investigation — and then the dropping of the major charges on the eve of trial — was “unconscionable.”