/**
  * Unwraps nested wrapped writers until the given writer class is found.
  *
  * @param writerClass Class of the desired nested writer. If null, the core writer (i.e., deepest
  *     wrapped writer) will be returned.
  * @param id Id to use as a basis when unwrapping any nested {@link ImageWriter}s. If null, the
  *     current id is used.
  */
 public IFormatWriter unwrap(Class<? extends IFormatWriter> writerClass, String id)
     throws FormatException, IOException {
   IFormatWriter w = this;
   while (w instanceof WriterWrapper || w instanceof ImageWriter) {
     if (writerClass != null && writerClass.isInstance(w)) break;
     if (w instanceof ImageWriter) {
       ImageWriter iw = (ImageWriter) w;
       w = id == null ? iw.getWriter() : iw.getWriter(id);
     } else w = ((WriterWrapper) w).getWriter();
   }
   if (writerClass != null && !writerClass.isInstance(w)) return null;
   return w;
 }