All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.api.rs.subtitles.AbstractIteratorWriter Maven / Gradle / Ivy

Go to download

Subtitles are related to media, but are implemented completely parallel. Classes to contain, parse, and assemble subtitle objects are here.

There is a newer version: 8.3.0
Show newest version
package nl.vpro.api.rs.subtitles;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Iterator;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;

import nl.vpro.domain.subtitles.Cue;
import nl.vpro.domain.subtitles.SubtitlesFormat;

/**
 * @author Michiel Meeuwissen
 * @since 5.1
 */
abstract class AbstractIteratorWriter implements MessageBodyWriter> {

    private final MediaType mediaType;
    private final String extension;

    public AbstractIteratorWriter(SubtitlesFormat format) {
        this.mediaType = MediaType.valueOf(format.getMediaType());
        this.extension = format.getExtension();
    }

    @Override
    public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return mediaType.isCompatible(this.mediaType) && Iterator.class.isAssignableFrom(type);

    }

    @Override
    public long getSize(Iterator cueIterator, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(Iterator cueIterator, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        cueIterator = Util.headers(cueIterator, httpHeaders, extension);
        stream(cueIterator, entityStream);
    }

    protected abstract void stream(Iterator cueIterator, OutputStream entityStream) throws IOException;



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy