A prisoner committed to the county jail for examination, or upon conviction for a public offense, must be actually confined in the jail until legally discharged; and if the prisoner is permitted to go at large out of the jail, except by virtue of a legal order or process, it is an escape; provided, however, that during the pendency of a criminal proceeding, the court before which said proceeding is pending may make a legal order, good cause appearing therefor, for the removal of the prisoner from the county jail in custody of the sheriff. In courts where there is a marshal, the marshal shall maintain custody of such prisoner while the prisoner is in the court facility pursuant to such court order. The superior court of the county may make a legal order, good cause appearing therefor, for the removal of prisoners confined in the county jail, after conviction, in the custody of the sheriff.
If facilities are no longer available in the county jail due to crowded conditions, a sheriff may transfer a person committed to the county jail upon conviction for a public offense to facilities which are available in the city jail, as provided for in Section 4004.5.
Ca. Pen. Code § 4004