If the accused is in the custody of a sheriff, or other officer, by virtue of an execution against his person, or by virtue of a mandate for any other contempt or misconduct, or a commitment on a criminal charge the court, upon proof of the facts, may issue a writ of habeas corpus, directed to the officer, requiring him to bring the accused before it, to answer for the offense charged. The officer to whom the writ is directed, or upon whom it is served, must bring him before the court, and detain him at the place where the court is sitting, until the further order of the court.
N.Y. Jud. Law § 767