code: private void writeObject(final ObjectOutputStream objectOutputStream) throws IOException {
          final ArrayList<L> serializableListeners = new ArrayList<>();
  
          // don't just rely on instanceof Serializable:
          ObjectOutputStream testObjectOutputStream = new ObjectOutputStream(new ByteArrayOutputStream());
          for (final L listener : listeners) {
              try {
                  testObjectOutputStream.writeObject(listener);
                  serializableListeners.add(listener);
              } catch (final IOException exception) {
                  //recreate test stream in case of indeterminate state
                  testObjectOutputStream = new ObjectOutputStream(new ByteArrayOutputStream());
              }
          }
          /*
           * we can reconstitute everything we need from an array of our listeners,
           * which has the additional advantage of typically requiring less storage than a list:
           */
          objectOutputStream.writeObject(serializableListeners.toArray(prototypeArray));
      }
docs: Serialize.
  @param objectOutputStream the output stream
  @throws IOException if an IO error occurs