< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 357                 iis.seek(savePos);
 358                 haveSeeked = true;
 359                 streamMetadata = new JPEGMetadata(true, false,
 360                                                   iis, this);
 361                 long pos = iis.getStreamPosition();
 362                 if (debug) {
 363                     System.out.println
 364                         ("pos after constructing stream metadata is " + pos);
 365                 }
 366             }
 367             // Now we are at the first image if there are any, so add it
 368             // to the list
 369             if (hasNextImage()) {
 370                 imagePositions.add(iis.getStreamPosition());
 371             }
 372         } else { // Not tables only, so add original pos to the list
 373             imagePositions.add(savePos);
 374             // And set current image since we've read it now
 375             currentImage = 0;
 376         }
 377         if (seekForwardOnly) {




 378             Long pos = imagePositions.get(imagePositions.size()-1);
 379             iis.flushBefore(pos.longValue());
 380         }
 381         tablesOnlyChecked = true;
 382     }
 383 
 384     public int getNumImages(boolean allowSearch) throws IOException {
 385         setThreadLock();
 386         try { // locked thread
 387             cbLock.check();
 388 
 389             return getNumImagesOnThread(allowSearch);
 390         } finally {
 391             clearThreadLock();
 392         }
 393     }
 394 
 395     private void skipPastImage(int imageIndex) {
 396         cbLock.lock();
 397         try {


 474     }
 475 
 476     /**
 477      * Sets the input stream to the start of the requested image.
 478      * <pre>
 479      * @exception IllegalStateException if the input source has not been
 480      * set.
 481      * @exception IndexOutOfBoundsException if the supplied index is
 482      * out of bounds.
 483      * </pre>
 484      */
 485     private void gotoImage(int imageIndex) throws IOException {
 486         if (iis == null) {
 487             throw new IllegalStateException("Input not set");
 488         }
 489         if (imageIndex < minIndex) {
 490             throw new IndexOutOfBoundsException();
 491         }
 492         if (!tablesOnlyChecked) {
 493             checkTablesOnly();





 494         }
 495         if (imageIndex < imagePositions.size()) {
 496             iis.seek(imagePositions.get(imageIndex).longValue());
 497         } else {
 498             // read to start of image, saving positions
 499             // First seek to the last position we already have, and skip the
 500             // entire image
 501             Long pos = imagePositions.get(imagePositions.size()-1);
 502             iis.seek(pos.longValue());
 503             skipImage();
 504             // Now add all intervening positions, skipping images
 505             for (int index = imagePositions.size();
 506                  index <= imageIndex;
 507                  index++) {
 508                 // Is there an image?
 509                 if (!hasNextImage()) {
 510                     throw new IndexOutOfBoundsException();
 511                 }
 512                 pos = iis.getStreamPosition();
 513                 imagePositions.add(pos);


   1 /*
   2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 357                 iis.seek(savePos);
 358                 haveSeeked = true;
 359                 streamMetadata = new JPEGMetadata(true, false,
 360                                                   iis, this);
 361                 long pos = iis.getStreamPosition();
 362                 if (debug) {
 363                     System.out.println
 364                         ("pos after constructing stream metadata is " + pos);
 365                 }
 366             }
 367             // Now we are at the first image if there are any, so add it
 368             // to the list
 369             if (hasNextImage()) {
 370                 imagePositions.add(iis.getStreamPosition());
 371             }
 372         } else { // Not tables only, so add original pos to the list
 373             imagePositions.add(savePos);
 374             // And set current image since we've read it now
 375             currentImage = 0;
 376         }
 377         // If imagePositions list doesn't contain any of the image stream
 378         // starting position(i.e tables-only image) we should not try to access
 379         // imagePositions.size() as it done below, because it will lead to
 380         // IndexOutOfBoundsException with index -1.
 381         if (seekForwardOnly && (!(imagePositions.isEmpty()))) {
 382             Long pos = imagePositions.get(imagePositions.size()-1);
 383             iis.flushBefore(pos.longValue());
 384         }
 385         tablesOnlyChecked = true;
 386     }
 387 
 388     public int getNumImages(boolean allowSearch) throws IOException {
 389         setThreadLock();
 390         try { // locked thread
 391             cbLock.check();
 392 
 393             return getNumImagesOnThread(allowSearch);
 394         } finally {
 395             clearThreadLock();
 396         }
 397     }
 398 
 399     private void skipPastImage(int imageIndex) {
 400         cbLock.lock();
 401         try {


 478     }
 479 
 480     /**
 481      * Sets the input stream to the start of the requested image.
 482      * <pre>
 483      * @exception IllegalStateException if the input source has not been
 484      * set.
 485      * @exception IndexOutOfBoundsException if the supplied index is
 486      * out of bounds.
 487      * </pre>
 488      */
 489     private void gotoImage(int imageIndex) throws IOException {
 490         if (iis == null) {
 491             throw new IllegalStateException("Input not set");
 492         }
 493         if (imageIndex < minIndex) {
 494             throw new IndexOutOfBoundsException();
 495         }
 496         if (!tablesOnlyChecked) {
 497             checkTablesOnly();
 498         }
 499         // We should not try to read image information from an input stream
 500         // which only contains tables-only(StreamMetadata) information.
 501         if (imagePositions.isEmpty()) {
 502             throw new IIOException("No image data present to read");
 503         }
 504         if (imageIndex < imagePositions.size()) {
 505             iis.seek(imagePositions.get(imageIndex).longValue());
 506         } else {
 507             // read to start of image, saving positions
 508             // First seek to the last position we already have, and skip the
 509             // entire image
 510             Long pos = imagePositions.get(imagePositions.size()-1);
 511             iis.seek(pos.longValue());
 512             skipImage();
 513             // Now add all intervening positions, skipping images
 514             for (int index = imagePositions.size();
 515                  index <= imageIndex;
 516                  index++) {
 517                 // Is there an image?
 518                 if (!hasNextImage()) {
 519                     throw new IndexOutOfBoundsException();
 520                 }
 521                 pos = iis.getStreamPosition();
 522                 imagePositions.add(pos);


< prev index next >