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 the image positions list is empty as in the case of a tables-only
378 // stream, then attempting to access the element at index
379 // imagePositions.size() - 1 will cause an IndexOutOfBoundsException.
380 if (seekForwardOnly && !imagePositions.isEmpty()) {
381 Long pos = imagePositions.get(imagePositions.size()-1);
382 iis.flushBefore(pos.longValue());
383 }
384 tablesOnlyChecked = true;
385 }
386
387 public int getNumImages(boolean allowSearch) throws IOException {
388 setThreadLock();
389 try { // locked thread
390 cbLock.check();
391
392 return getNumImagesOnThread(allowSearch);
393 } finally {
394 clearThreadLock();
395 }
396 }
397
398 private void skipPastImage(int imageIndex) {
399 cbLock.lock();
400 try {
477 }
478
479 /**
480 * Sets the input stream to the start of the requested image.
481 * <pre>
482 * @exception IllegalStateException if the input source has not been
483 * set.
484 * @exception IndexOutOfBoundsException if the supplied index is
485 * out of bounds.
486 * </pre>
487 */
488 private void gotoImage(int imageIndex) throws IOException {
489 if (iis == null) {
490 throw new IllegalStateException("Input not set");
491 }
492 if (imageIndex < minIndex) {
493 throw new IndexOutOfBoundsException();
494 }
495 if (!tablesOnlyChecked) {
496 checkTablesOnly();
497 }
498 // If the image positions list is empty as in the case of a tables-only
499 // stream, then no image data can be read.
500 if (imagePositions.isEmpty()) {
501 throw new IIOException("No image data present to read");
502 }
503 if (imageIndex < imagePositions.size()) {
504 iis.seek(imagePositions.get(imageIndex).longValue());
505 } else {
506 // read to start of image, saving positions
507 // First seek to the last position we already have, and skip the
508 // entire image
509 Long pos = imagePositions.get(imagePositions.size()-1);
510 iis.seek(pos.longValue());
511 skipImage();
512 // Now add all intervening positions, skipping images
513 for (int index = imagePositions.size();
514 index <= imageIndex;
515 index++) {
516 // Is there an image?
517 if (!hasNextImage()) {
518 throw new IndexOutOfBoundsException();
519 }
520 pos = iis.getStreamPosition();
521 imagePositions.add(pos);
|