| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.util; |
| 15 |
|
| 16 |
import java.awt.image.BufferedImage; |
| 17 |
import java.io.*; |
| 18 |
import java.net.URL; |
| 19 |
import java.nio.file.Files; |
| 20 |
import java.util.*; |
| 21 |
import java.util.regex.*; |
| 22 |
|
| 23 |
import javax.imageio.ImageIO; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.core.Observable; |
| 27 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
/** |
| 31 |
* Parses the main sources, locates the {@code <img>} tags, downloads |
| 32 |
* the referenced image and checks if the scaled dimensions are correct. |
| 33 |
*/ |
| 34 |
public final class MarbleDimensions { |
| 35 |
|
| 36 |
/** Helper program. */ |
| 37 |
private MarbleDimensions() { |
| 38 |
throw new IllegalStateException("No instances!"); |
| 39 |
} |
| 40 |
|
| 41 |
public static void main(String[] args) throws Throwable { |
| 42 |
Pattern p = Pattern.compile("\\s*\\*\\s*\\<img\\s+width\\=('|\")(\\d+)('|\")\\s+height\\=('|\")(\\d+)('|\")\\s+src\\=('|\")(.+?)('|\").*"); |
| 43 |
|
| 44 |
Map<String, Integer[]> dimensions = new HashMap<>(); |
| 45 |
|
| 46 |
for (Class<?> clazz : CLASSES) { |
| 47 |
String simpleName = clazz.getSimpleName(); |
| 48 |
System.out.println(simpleName); |
| 49 |
System.out.println("----"); |
| 50 |
String packageName = clazz.getPackage().getName(); |
| 51 |
|
| 52 |
File f = TestHelper.findSource(clazz.getSimpleName(), packageName); |
| 53 |
if (f == null) { |
| 54 |
System.err.println("Unable to locate " + clazz); |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
List<String> lines = Files.readAllLines(f.toPath()); |
| 59 |
|
| 60 |
for (int i = 0; i < lines.size(); i++) { |
| 61 |
Matcher m = p.matcher(lines.get(i)); |
| 62 |
if (m.matches()) { |
| 63 |
int width = Integer.parseInt(m.group(2)); |
| 64 |
int height = Integer.parseInt(m.group(5)); |
| 65 |
String url = m.group(8); |
| 66 |
|
| 67 |
Integer[] imageDim = dimensions.get(url); |
| 68 |
if (imageDim == null) { |
| 69 |
Thread.sleep(SLEEP_PER_IMAGE_MILLIS); |
| 70 |
|
| 71 |
try { |
| 72 |
BufferedImage bimg = ImageIO.read(new URL(url)); |
| 73 |
|
| 74 |
if (bimg == null) { |
| 75 |
throw new IOException("not found"); |
| 76 |
} |
| 77 |
imageDim = new Integer[] { 0, 0 }; |
| 78 |
imageDim[0] = bimg.getWidth(); |
| 79 |
imageDim[1] = bimg.getHeight(); |
| 80 |
|
| 81 |
dimensions.put(url, imageDim); |
| 82 |
} catch (IOException ex) { |
| 83 |
System.err.printf("%s => %s%n", url, ex); |
| 84 |
System.err.printf(" at %s.%s.method(%s.java:%d)%n", packageName, simpleName, simpleName, i + 1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (imageDim != null) { |
| 89 |
int expectedHeight = (int)Math.round(1.0 * width / imageDim[0] * imageDim[1]); |
| 90 |
|
| 91 |
if (expectedHeight != height) { |
| 92 |
System.out.printf(" %d => %d%n", height, expectedHeight); |
| 93 |
System.out.printf(" at %s.%s.method(%s.java:%d)%n", packageName, simpleName, simpleName, i + 1); |
| 94 |
} |
| 95 |
} |
| 96 |
// System.out.printf("%d: %d x %d => %s%n", i + 1, width, height, url); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static final int SLEEP_PER_IMAGE_MILLIS = 25; |
| 103 |
|
| 104 |
static final Class<?>[] CLASSES = { |
| 105 |
Flowable.class, Observable.class, Maybe.class, Single.class, Completable.class, ParallelFlowable.class |
| 106 |
}; |
| 107 |
} |