SAN JOSE — In a scathing opinion published Thursday, a state appellate court reversed a judgment against a felon accused of being a sexually violent predator, saying the prosecutor handling the case — now District Attorney Jeff Rosen’s right-hand man — engaged in a “pervasive pattern” of misconduct.

The misconduct finding against Chief Assistant District Attorney Jay Boyarsky is the first black mark against Rosen’s administration since he took office two years ago on an ethics platform and appointed Boyarsky as his top assistant.

Rosen handled the situation Thursday with the same aplomb he demonstrated as a homicide prosecutor to defuse potential courtroom crises. Boyarsky, who is also Rosen’s best friend, tried the case when he was a line prosecutor before Rosen came into office.

“We respect the court’s decision,” Rosen said. “Any prosecutor in my office may err, and when we do, we learn from it and improve.”

The ruling, written by Conrad Rushing, the presiding judge of the 6th District Court of Appeal, stems from a civil court hearing in Santa Clara County in which Dariel Shazier was involuntarily committed to a state mental hospital on the grounds that he was a sexually violent predator.

Boyarsky erred by asking improper questions of the witnesses that elicited inflammatory answers and by making improper arguments to the jury, Rushing wrote. For instance, Boyarsky implied without offering any evidence that Shazier had molested other boys. The prosecutor also essentially asked jurors what their friends and family would think of them if they found Shazier wasn’t a predator.

“This is not a case in which the prosecutor engaged in a few minor incidents of improper conduct,” Rushing wrote. “Rather, the prosecutor engaged in a pervasive pattern of inappropriate questions, comment and argument throughout the entire trial.”

Boyarsky said he accepts the court’s ruling about his conduct, though he made his arguments in “good faith.”

“Based on the court’s opinion, if I had it to do over again,” he said, “I would make my arguments differently.”

Although the ruling reverses the involuntary civil commitment order, Shazier will remain at Coalinga State Hospital while the case is pending. Rosen noted Shazier has a “serious history of sexually preying on young teenagers,” and said his office will try him again if doctors conclude he is a predator. Under another scenario, the state Attorney General’s Office could appeal the ruling to the state Supreme Court, which could strike down the appellate decision, allowing the involuntary commitment to a mental hospital to stand.

The case dates back to 1994, when Shazier pleaded guilty to sodomy with a minor under 14, sodomy with a minor under 18 and oral copulation where the victim is unable to resist due to an intoxicating substance. He was sentenced to 17 years and eight months in prison.

Shortly before he was to be released, the DA’s Office sought to have him involuntarily committed to a state mental hospital on the grounds he was a sexually violent predator. The first attempt resulted in a mistrial when the jury voted 11-1 in favor of finding he was a predator. The second attempt, by now-disgraced former prosecutor Ben Field, was also reversed on the grounds of prosecutorial misconduct.

To qualify Shazier as a predator, Boyarsky had to show he had “a diagnosable mental disorder that predisposes him to sexual violence.”

Two of the two state doctors testifying for Boyarsky said Shazier has hebephilia, a sexual attraction to teenage boys who have attained puberty. However, hebephilia is not classified as a mental illness, Rushing noted. One of the doctors also testified that the defendant was not a pedophile and was not aroused by force or violence, and that the only reason his prior crimes were considered nonconsensual was because the victims were minors who could not legally consent.

Rushing said that Shazier presented a “vigorous defense,” including evidence that he had spent the last 15 years while incarcerated seeking every voluntary treatment available. The justice also criticized the trial judge, Alfonso Fernandez, for overruling all of the many “well-taken” objections made by Shazier’s defense attorney, Deputy Public Defender Patrick Hoopes.

Dallas Sacher, director of the Sixth District Appellate Program, which handled the appeal, praised the ruling.

“Nobody in their right mind wants people on the street doing this to boys,” Sacher said. “But creating a make-believe genre of crazy people isn’t right either. The ends do not justify the means.”

A spokesman for the Attorney General said the office is reviewing the opinion and hasn’t decided whether to appeal it to the state Supreme Court.

“We argued in our appellate court brief that none of the cited instances constituted prosecutorial misconduct and reversal was not required because it is not ‘reasonably probable that a result more favorable to the defendant would have occurred’ absent the misconduct.”

Contact Tracey Kaplan at 408-278-3482. Follow her at Twitter.com/tkaplanreport.