Search in sources :

Example 21 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project openj9 by eclipse.

the class J9DDRStructureStore method get.

/**
 * @param key
 * @return An InputStream open and positioned at the start of the J9DDR structure the blob associated with key.  null if key is not contained in the database
 * @throws IOException - if there is a problem retrieving the blob
 */
public ImageInputStream get(StructureKey key) throws IOException {
    String digest = keyMap.get(key);
    if (digest == null) {
        return null;
    }
    ZipFile zipFile = new ZipFile(getZipFile(key, digest));
    ZipEntry entry = zipFile.getEntry(getZipEntry(key, digest).getName());
    InputStream entryStream = zipFile.getInputStream(entry);
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream output = new ByteArrayOutputStream((int) entry.getSize());
    while (true) {
        int bytesRead = entryStream.read(buffer);
        if (bytesRead == -1) {
            break;
        }
        output.write(buffer, 0, bytesRead);
    }
    zipFile.close();
    return new MemoryCacheImageInputStream(new ByteArrayInputStream(output.toByteArray()));
}
Also used : ZipFile(java.util.zip.ZipFile) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project spring-security-oauth by spring-projects.

the class SparklrController method photo.

@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id, HttpServletRequest request) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.IMAGE_JPEG));
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project spring-security-oauth by spring-projects.

the class SparklrRedirectController method photo.

@RequestMapping("/sparklr/redirect/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project spring-security-oauth by spring-projects.

the class SparklrController method photo.

@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project pdfbox by apache.

the class LZWFilter method doLZWDecode.

private void doLZWDecode(InputStream encoded, OutputStream decoded, int earlyChange) throws IOException {
    List<byte[]> codeTable = new ArrayList<>();
    int chunk = 9;
    final MemoryCacheImageInputStream in = new MemoryCacheImageInputStream(encoded);
    long nextCommand;
    long prevCommand = -1;
    try {
        while ((nextCommand = in.readBits(chunk)) != EOD) {
            if (nextCommand == CLEAR_TABLE) {
                chunk = 9;
                codeTable = createCodeTable();
                prevCommand = -1;
            } else {
                if (nextCommand < codeTable.size()) {
                    byte[] data = codeTable.get((int) nextCommand);
                    byte firstByte = data[0];
                    decoded.write(data);
                    if (prevCommand != -1) {
                        checkIndexBounds(codeTable, prevCommand, in);
                        data = codeTable.get((int) prevCommand);
                        byte[] newData = Arrays.copyOf(data, data.length + 1);
                        newData[data.length] = firstByte;
                        codeTable.add(newData);
                    }
                } else {
                    checkIndexBounds(codeTable, prevCommand, in);
                    byte[] data = codeTable.get((int) prevCommand);
                    byte[] newData = Arrays.copyOf(data, data.length + 1);
                    newData[data.length] = data[0];
                    decoded.write(newData);
                    codeTable.add(newData);
                }
                chunk = calculateChunk(codeTable.size(), earlyChange);
                prevCommand = nextCommand;
            }
        }
    } catch (EOFException ex) {
        LOG.warn("Premature EOF in LZW stream, EOD code missing", ex);
    }
    decoded.flush();
}
Also used : ArrayList(java.util.ArrayList) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) EOFException(java.io.EOFException)

Aggregations

MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)54 ImageInputStream (javax.imageio.stream.ImageInputStream)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ImageReader (javax.imageio.ImageReader)22 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 BufferedImage (java.awt.image.BufferedImage)17 FileInputStream (java.io.FileInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 COSDictionary (org.apache.pdfbox.cos.COSDictionary)8 EOFException (java.io.EOFException)7 ArrayList (java.util.ArrayList)6 COSStream (org.apache.pdfbox.cos.COSStream)6 PDRange (org.apache.pdfbox.pdmodel.common.PDRange)6 Point (java.awt.Point)5 Point2D (java.awt.geom.Point2D)5 ImageReadParam (javax.imageio.ImageReadParam)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 Paint (java.awt.Paint)4 Size (org.olat.core.commons.services.image.Size)4