diff --git a/.github/scripts/generate-quality-report.py b/.github/scripts/generate-quality-report.py index 7855cad769..f6565620fb 100755 --- a/.github/scripts/generate-quality-report.py +++ b/.github/scripts/generate-quality-report.py @@ -763,6 +763,7 @@ def main() -> None: if spotbugs: forbidden_rules = { "NP_ALWAYS_NULL", + "NP_NULL_PARAM_DEREF", "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", @@ -772,10 +773,12 @@ def main() -> None: "IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD", "LI_LAZY_INIT_STATIC", "RpC_REPEATED_CONDITIONAL_TEST", + "NS_NON_SHORT_CIRCUIT", "ES_COMPARING_PARAMETER_STRING_WITH_EQ", "FE_FLOATING_POINT_EQUALITY", "FE_TEST_IF_EQUAL_TO_NOT_A_NUMBER", "ICAST_IDIV_CAST_TO_DOUBLE", + "ICAST_QUESTIONABLE_UNSIGNED_RIGHT_SHIFT", "SA_FIELD_SELF_ASSIGNMENT", "UC_USELESS_CONDITION", "UC_USELESS_OBJECT", @@ -787,7 +790,9 @@ def main() -> None: "EQ_DOESNT_OVERRIDE_EQUALS", "CO_COMPARETO_INCORRECT_FLOATING", "DL_SYNCHRONIZATION_ON_SHARED_CONSTANT", + "SSD_DO_NOT_USE_INSTANCE_LOCK_ON_SHARED_STATIC_DATA", "DLS_DEAD_LOCAL_STORE", + "DLS_DEAD_LOCAL_STORE_OF_NULL", "DM_NUMBER_CTOR", "DMI_INVOKING_TOSTRING_ON_ARRAY", "EC_NULL_ARG", @@ -815,16 +820,32 @@ def main() -> None: "NM_CONFUSING", "NM_FIELD_NAMING_CONVENTION", "NM_METHOD_NAMING_CONVENTION", + "NN_NAKED_NOTIFY", "NO_NOTIFY_NOT_NOTIFYALL", "NP_LOAD_OF_KNOWN_NULL_VALUE", "NP_BOOLEAN_RETURN_NULL", + "RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN", + "OS_OPEN_STREAM", "REFLC_REFLECTION_MAY_INCREASE_ACCESSIBILITY_OF_CLASS", "REC_CATCH_EXCEPTION", "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", + "INT_VACUOUS_COMPARISON", + "DM_STRING_TOSTRING", + "HE_HASHCODE_USE_OBJECT_EQUALS", + "IM_BAD_CHECK_FOR_ODD", + "IM_AVERAGE_COMPUTATION_COULD_OVERFLOW", + "INT_VACUOUS_BIT_OPERATION", + "ICAST_INT_2_LONG_AS_INSTANT", + "ICAST_INT_CAST_TO_FLOAT_PASSED_TO_ROUND", + "IT_NO_SUCH_ELEMENT", + "FL_FLOATS_AS_LOOP_COUNTERS", "UI_INHERITANCE_UNSAFE_GETRESOURCE", + "IS2_INCONSISTENT_SYNC", + "RR_NOT_CHECKED", "URF_UNREAD_FIELD", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", + "UR_UNINIT_READ", "UUF_UNUSED_FIELD", "UWF_NULL_FIELD", "UW_UNCOND_WAIT", @@ -832,9 +853,15 @@ def main() -> None: "SIC_INNER_SHOULD_BE_STATIC_ANON", "SS_SHOULD_BE_STATIC", "UPM_UNCALLED_PRIVATE_METHOD", + "RV_RETURN_VALUE_IGNORED_INFERRED", "RV_CHECK_FOR_POSITIVE_INDEXOF", "SF_SWITCH_FALLTHROUGH", - "SIC_INNER_SHOULD_BE_STATIC_NEEDS_THIS" + "SIC_INNER_SHOULD_BE_STATIC_NEEDS_THIS", + "SA_FIELD_DOUBLE_ASSIGNMENT", + "SA_FIELD_SELF_COMPARISON", + "SR_NOT_CHECKED", + "SWL_SLEEP_WITH_LOCK_HELD", + "UC_USELESS_CONDITION_TYPE" } def _is_exempt(f: Finding) -> bool: @@ -851,6 +878,10 @@ def _is_exempt(f: Finding) -> bool: return True if f.rule == "URF_UNREAD_FIELD" and "GridBagLayoutInfo" in loc: return True + if f.rule == "NN_NAKED_NOTIFY" and "Display.java" in loc: + return True + if f.rule == "ICAST_QUESTIONABLE_UNSIGNED_RIGHT_SHIFT" and "Deflate.java" in loc: + return True return False @@ -864,6 +895,18 @@ def _is_exempt(f: Finding) -> bool: print(f" - {v.rule}: {v.location} - {v.message}") exit(1) + pmd = parse_pmd() + if pmd: + forbidden_pmd_rules = { + "ClassWithOnlyPrivateConstructorsShouldBeFinal", + } + violations = [f for f in pmd.findings if f.rule in forbidden_pmd_rules] + if violations: + print("\n❌ Build failed due to forbidden PMD violations:") + for v in violations: + print(f" - {v.rule}: {v.location} - {v.message}") + exit(1) + if __name__ == "__main__": main() diff --git a/CodenameOne/src/com/codename1/capture/Capture.java b/CodenameOne/src/com/codename1/capture/Capture.java index 4b65da9637..68f4939ce6 100644 --- a/CodenameOne/src/com/codename1/capture/Capture.java +++ b/CodenameOne/src/com/codename1/capture/Capture.java @@ -247,7 +247,7 @@ public static void captureVideo(ActionListener response) { static class CallBack implements ActionListener, Runnable { String url; - private boolean completed; + private volatile boolean completed; private int targetWidth = -1; private int targetHeight = -1; @@ -257,8 +257,8 @@ public void actionPerformed(ActionEvent evt) { } else { url = (String) evt.getSource(); } - completed = true; synchronized (this) { + completed = true; this.notifyAll(); } } diff --git a/CodenameOne/src/com/codename1/charts/util/MathHelper.java b/CodenameOne/src/com/codename1/charts/util/MathHelper.java index 0998e7dd2b..02ce7c9eed 100644 --- a/CodenameOne/src/com/codename1/charts/util/MathHelper.java +++ b/CodenameOne/src/com/codename1/charts/util/MathHelper.java @@ -26,7 +26,7 @@ /** * Utility class for math operations. */ -public class MathHelper { +public final class MathHelper { /** A value that is used a null value. */ public static final double NULL_VALUE = -Double.MAX_VALUE + 1; /** diff --git a/CodenameOne/src/com/codename1/charts/views/PieSegment.java b/CodenameOne/src/com/codename1/charts/views/PieSegment.java index caf8bee97e..756bba6087 100644 --- a/CodenameOne/src/com/codename1/charts/views/PieSegment.java +++ b/CodenameOne/src/com/codename1/charts/views/PieSegment.java @@ -52,9 +52,10 @@ public boolean isInSegment(double angle) { double cAngle = angle % 360; double startAngle = mStartAngle; double stopAngle = mEndAngle; - while (stopAngle > 360) { - startAngle -= 360; - stopAngle -= 360; + if (stopAngle > 360) { + int rotations = (int) Math.floor(stopAngle / 360d); + startAngle -= 360 * rotations; + stopAngle -= 360 * rotations; } return cAngle >= startAngle && cAngle <= stopAngle; } diff --git a/CodenameOne/src/com/codename1/charts/views/RadarChart.java b/CodenameOne/src/com/codename1/charts/views/RadarChart.java index 7adb92969e..7bc8de3671 100644 --- a/CodenameOne/src/com/codename1/charts/views/RadarChart.java +++ b/CodenameOne/src/com/codename1/charts/views/RadarChart.java @@ -112,8 +112,9 @@ public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint paint.setColor(ColorUtil.GRAY); float thisRad = (float) Math.toRadians(90 - currentAngle); float nextRad = (float) Math.toRadians(90 - (currentAngle + angle)); - for (double level = 0; level <= 1d; level += decCoef) { // PMD Fix: DontUseFloatTypeForLoopIndices switched to double - float levelFactor = (float) level; + int levelSteps = (int) Math.round(1d / decCoef); + for (int level = 0; level <= levelSteps; level++) { + float levelFactor = (float) (level * decCoef); float thisX = (float) (centerX - Math.sin(thisRad) * radius * levelFactor); float thisY = (float) (centerY - Math.cos(thisRad) * radius * levelFactor); float nextX = (float) (centerX - Math.sin(nextRad) * radius * levelFactor); diff --git a/CodenameOne/src/com/codename1/components/ClearableTextField.java b/CodenameOne/src/com/codename1/components/ClearableTextField.java index 02e1cfaa05..2c9dd37086 100644 --- a/CodenameOne/src/com/codename1/components/ClearableTextField.java +++ b/CodenameOne/src/com/codename1/components/ClearableTextField.java @@ -39,7 +39,7 @@ * * @author Shai Almog */ -public class ClearableTextField extends Container { +public final class ClearableTextField extends Container { private ClearableTextField() { super(new BorderLayout()); } diff --git a/CodenameOne/src/com/codename1/components/FileEncodedImage.java b/CodenameOne/src/com/codename1/components/FileEncodedImage.java index 6667675e3b..0e293fbe12 100644 --- a/CodenameOne/src/com/codename1/components/FileEncodedImage.java +++ b/CodenameOne/src/com/codename1/components/FileEncodedImage.java @@ -41,7 +41,7 @@ * * @author Shai Almog */ -public class FileEncodedImage extends EncodedImage { +public final class FileEncodedImage extends EncodedImage { private final String fileName; private final boolean keep; private byte[] data; diff --git a/CodenameOne/src/com/codename1/components/FileEncodedImageAsync.java b/CodenameOne/src/com/codename1/components/FileEncodedImageAsync.java index 1551d139b7..05e585eb89 100644 --- a/CodenameOne/src/com/codename1/components/FileEncodedImageAsync.java +++ b/CodenameOne/src/com/codename1/components/FileEncodedImageAsync.java @@ -41,7 +41,7 @@ * * @author Shai Almog */ -public class FileEncodedImageAsync extends EncodedImage { +public final class FileEncodedImageAsync extends EncodedImage { private static final Object LOCK = new Object(); private final String fileName; private boolean changePending; diff --git a/CodenameOne/src/com/codename1/components/InfiniteScrollAdapter.java b/CodenameOne/src/com/codename1/components/InfiniteScrollAdapter.java index 54986827ca..cb66480069 100644 --- a/CodenameOne/src/com/codename1/components/InfiniteScrollAdapter.java +++ b/CodenameOne/src/com/codename1/components/InfiniteScrollAdapter.java @@ -49,7 +49,7 @@ * * @author Shai Almog */ -public class InfiniteScrollAdapter { +public final class InfiniteScrollAdapter { private Container infiniteContainer; private Runnable fetchMore; private final Component ip; diff --git a/CodenameOne/src/com/codename1/components/ReplaceableImage.java b/CodenameOne/src/com/codename1/components/ReplaceableImage.java index 893ef939e0..382d483a48 100644 --- a/CodenameOne/src/com/codename1/components/ReplaceableImage.java +++ b/CodenameOne/src/com/codename1/components/ReplaceableImage.java @@ -32,7 +32,7 @@ * * @author Shai Almog */ -public class ReplaceableImage extends EncodedImage { +public final class ReplaceableImage extends EncodedImage { private boolean replaced; private byte[] data; private final boolean opaque; diff --git a/CodenameOne/src/com/codename1/components/StorageImage.java b/CodenameOne/src/com/codename1/components/StorageImage.java index 9486e7f15e..732c032c84 100644 --- a/CodenameOne/src/com/codename1/components/StorageImage.java +++ b/CodenameOne/src/com/codename1/components/StorageImage.java @@ -36,7 +36,7 @@ * * @author Shai Almog */ -public class StorageImage extends EncodedImage { +public final class StorageImage extends EncodedImage { private final String fileName; private final boolean keep; private byte[] data; diff --git a/CodenameOne/src/com/codename1/components/StorageImageAsync.java b/CodenameOne/src/com/codename1/components/StorageImageAsync.java index bc8454fc8c..b1d45a97f4 100644 --- a/CodenameOne/src/com/codename1/components/StorageImageAsync.java +++ b/CodenameOne/src/com/codename1/components/StorageImageAsync.java @@ -34,7 +34,7 @@ * * @author Shai Almog */ -public class StorageImageAsync extends EncodedImage { +public final class StorageImageAsync extends EncodedImage { private static final Object LOCK = new Object(); private final String fileName; private boolean changePending; diff --git a/CodenameOne/src/com/codename1/components/ToastBar.java b/CodenameOne/src/com/codename1/components/ToastBar.java index e79787908c..43a2ca62a4 100644 --- a/CodenameOne/src/com/codename1/components/ToastBar.java +++ b/CodenameOne/src/com/codename1/components/ToastBar.java @@ -82,7 +82,7 @@ * * @author shannah */ -public class ToastBar { +public final class ToastBar { /** * The default timeout for info/error messages diff --git a/CodenameOne/src/com/codename1/components/WebBrowser.java b/CodenameOne/src/com/codename1/components/WebBrowser.java index 50f4791d8b..0067d89f00 100644 --- a/CodenameOne/src/com/codename1/components/WebBrowser.java +++ b/CodenameOne/src/com/codename1/components/WebBrowser.java @@ -154,8 +154,8 @@ protected void readResponse(InputStream input) throws IOException { if (callback != null) { callback.streamReady(input, docInfo); } else { - response[0] = input; synchronized (LOCK) { + response[0] = input; LOCK.notifyAll(); } } diff --git a/CodenameOne/src/com/codename1/facebook/FaceBookAccess.java b/CodenameOne/src/com/codename1/facebook/FaceBookAccess.java index 1fcf018e80..0814951f53 100644 --- a/CodenameOne/src/com/codename1/facebook/FaceBookAccess.java +++ b/CodenameOne/src/com/codename1/facebook/FaceBookAccess.java @@ -53,7 +53,7 @@ * * @author Chen Fishbein */ -public class FaceBookAccess { +public final class FaceBookAccess { private static final String TEMP_STORAGE = "FaceBookAccesstmp"; private static String clientId = "132970916828080"; diff --git a/CodenameOne/src/com/codename1/facebook/FacebookRESTService.java b/CodenameOne/src/com/codename1/facebook/FacebookRESTService.java index f9c8ad68d1..82a6b05926 100644 --- a/CodenameOne/src/com/codename1/facebook/FacebookRESTService.java +++ b/CodenameOne/src/com/codename1/facebook/FacebookRESTService.java @@ -239,8 +239,11 @@ public void numericToken(double tok) { } public void keyValue(String key, String value) { + if (key == null) { + return; + } //make sure value is not null to prevent NPE - if (key != null && value == null) { + if (value == null) { value = ""; } getCurrent().put(key, value); diff --git a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java index 0fdaad117e..de5d7a74be 100644 --- a/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java +++ b/CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java @@ -1516,9 +1516,9 @@ public int getTranslateY(Object graphics) { * said color value. * * @param graphics the graphics context - * @param RGB the RGB value for the color. + * @param rgb the RGB value for the color. */ - public abstract void setColor(Object graphics, int RGB); + public abstract void setColor(Object graphics, int rgb); /** * Alpha value from 0-255 can be ignored for some operations @@ -3966,10 +3966,7 @@ public Rectangle getDisplaySafeArea(Rectangle rect) { * common constants in this class or be a user/implementation defined sound */ public void playBuiltinSound(String soundIdentifier) { - boolean played = playUserSound(soundIdentifier); - if (!played) { - return; - } + playUserSound(soundIdentifier); } /** @@ -3977,11 +3974,11 @@ public void playBuiltinSound(String soundIdentifier) { * * @param soundIdentifier the sound identifier which can match one of the * common constants in this class or be a user/implementation defined sound - * @return true if a user sound exists and was sent to playback */ - protected boolean playUserSound(String soundIdentifier) { - Object sound = builtinSounds.get(soundIdentifier); - return sound != null; + protected void playUserSound(String soundIdentifier) { + // TODO: Reintroduce builitin sound support + //Object sound = builtinSounds.get(soundIdentifier); + //return sound != null; //playAudio(sound); } @@ -7177,13 +7174,9 @@ protected final void pushReceived(String data) { * Sets the frequency for polling the server in case of polling based push notification * * @param freq the frequency in milliseconds + * @deprecated we no longer support push polling */ public void setPollingFrequency(int freq) { - if (callback != null && pollingThreadRunning) { - synchronized (callback) { - callback.notifyAll(); - } - } } /** diff --git a/CodenameOne/src/com/codename1/impl/CodenameOneThread.java b/CodenameOne/src/com/codename1/impl/CodenameOneThread.java index 66dd820798..021ed7a562 100644 --- a/CodenameOne/src/com/codename1/impl/CodenameOneThread.java +++ b/CodenameOne/src/com/codename1/impl/CodenameOneThread.java @@ -23,6 +23,7 @@ package com.codename1.impl; import com.codename1.io.Log; +import com.codename1.io.Util; import com.codename1.ui.Display; import java.io.DataInputStream; @@ -133,6 +134,8 @@ public void storeStackForException(Throwable t, int currentStackFrame) { * Prints the stack trace matching the given stack */ public String getStack(Throwable t) { + InputStream inp = null; + DataInputStream di = null; try { StringBuilder b = new StringBuilder(); int size; @@ -145,11 +148,11 @@ public String getStack(Throwable t) { } String[] stk = new String[size]; - InputStream inp = Display.getInstance().getResourceAsStream(getClass(), "/methodData.dat"); + inp = Display.getInstance().getResourceAsStream(getClass(), "/methodData.dat"); if (inp == null) { return t.toString(); } - DataInputStream di = new DataInputStream(inp); + di = new DataInputStream(inp); int totalAmount = di.readInt(); String lastClass = ""; for (int x = 0; x < totalAmount; x++) { @@ -172,6 +175,9 @@ public String getStack(Throwable t) { return b.toString(); } catch (IOException ex) { ex.printStackTrace(); + } finally { + Util.cleanup(di); + Util.cleanup(inp); } return "Failed in stack generation for " + t; } diff --git a/CodenameOne/src/com/codename1/io/BufferedInputStream.java b/CodenameOne/src/com/codename1/io/BufferedInputStream.java index deba842715..175b3e2c35 100644 --- a/CodenameOne/src/com/codename1/io/BufferedInputStream.java +++ b/CodenameOne/src/com/codename1/io/BufferedInputStream.java @@ -378,14 +378,18 @@ private int read1(byte[] b, int off, int len) throws IOException { return cnt; } - private synchronized void yieldTime() { + private void yieldTime() { long time = System.currentTimeMillis(); - if (time - elapsedSinceLastYield > 300) { - try { - Thread.sleep(yield); - } catch (InterruptedException ex) { + long sleepDuration = 0; + synchronized (this) { + if (time - elapsedSinceLastYield > 300) { + elapsedSinceLastYield = time; + sleepDuration = yield; } - elapsedSinceLastYield = time; + } + if (sleepDuration > 0) { + int sleepMs = sleepDuration > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) sleepDuration; + Util.sleep(sleepMs); } } @@ -637,18 +641,14 @@ public void close() throws IOException { if (connection != null) { Util.getImplementation().cleanup(connection); } - byte[] buffer; - while ((buffer = buf) != null) { - if (buf == buffer) { //bufUpdater.compareAndSet(this, buffer, null)) { - buf = null; - InputStream input = in; - in = null; - if (input != null) { - input.close(); - } - return; + byte[] buffer = buf; + if (buffer != null) { + buf = null; + InputStream input = in; + in = null; + if (input != null) { + input.close(); } - // Else retry in case a new buf was CASed in fill() } } diff --git a/CodenameOne/src/com/codename1/io/FileSystemStorage.java b/CodenameOne/src/com/codename1/io/FileSystemStorage.java index d512f78f07..f5134002ca 100644 --- a/CodenameOne/src/com/codename1/io/FileSystemStorage.java +++ b/CodenameOne/src/com/codename1/io/FileSystemStorage.java @@ -47,7 +47,7 @@ * * @author Shai Almog */ -public class FileSystemStorage { +public final class FileSystemStorage { /** * Represents the type for the get root type method, this type generally represents the main * phone memory diff --git a/CodenameOne/src/com/codename1/io/MultipartRequest.java b/CodenameOne/src/com/codename1/io/MultipartRequest.java index 7a383cd34a..ff026cc1ef 100644 --- a/CodenameOne/src/com/codename1/io/MultipartRequest.java +++ b/CodenameOne/src/com/codename1/io/MultipartRequest.java @@ -269,9 +269,9 @@ protected long calculateContentLength() { length += key.length(); if (ignoreEncoding.contains(key)) { try { - length += value.toString().getBytes("UTF-8").length; + length += ((String) value).getBytes("UTF-8").length; } catch (UnsupportedEncodingException ex) { - length += StringUtil.getBytes(value.toString()).length; + length += StringUtil.getBytes((String) value).length; } } else { if (base64Binaries) { @@ -287,9 +287,9 @@ protected long calculateContentLength() { length += key.length(); if (ignoreEncoding.contains(key)) { try { - length += s.toString().getBytes("UTF-8").length; + length += s.getBytes("UTF-8").length; } catch (UnsupportedEncodingException ex) { - length += StringUtil.getBytes(value.toString()).length; + length += StringUtil.getBytes(s).length; } } else { if (base64Binaries) { @@ -534,4 +534,4 @@ public int hashCode() { result = 31 * result + (args != null ? args.hashCode() : 0); return result; } -} \ No newline at end of file +} diff --git a/CodenameOne/src/com/codename1/io/NetworkManager.java b/CodenameOne/src/com/codename1/io/NetworkManager.java index a96c460b8f..f2e6ca50a7 100644 --- a/CodenameOne/src/com/codename1/io/NetworkManager.java +++ b/CodenameOne/src/com/codename1/io/NetworkManager.java @@ -55,7 +55,7 @@ * * @author Shai Almog */ -public class NetworkManager { +public final class NetworkManager { /** * Indicates an unknown access point type */ @@ -330,8 +330,8 @@ public void shutdown() { } } } - networkThreads = null; synchronized (LOCK) { + networkThreads = null; LOCK.notifyAll(); } @@ -970,10 +970,10 @@ public void run() { if (!runCurrentRequest(currentRequest)) { continue; } - currentRequest = null; // wakeup threads waiting for the completion of this network operation synchronized (LOCK) { + currentRequest = null; LOCK.notifyAll(); } } else { diff --git a/CodenameOne/src/com/codename1/io/Socket.java b/CodenameOne/src/com/codename1/io/Socket.java index f4e0fbf733..140619266a 100644 --- a/CodenameOne/src/com/codename1/io/Socket.java +++ b/CodenameOne/src/com/codename1/io/Socket.java @@ -35,7 +35,7 @@ * * @author Shai Almog */ -public class Socket { +public final class Socket { private Socket() { } diff --git a/CodenameOne/src/com/codename1/io/Util.java b/CodenameOne/src/com/codename1/io/Util.java index 5cfcf6dbc5..a327025cda 100644 --- a/CodenameOne/src/com/codename1/io/Util.java +++ b/CodenameOne/src/com/codename1/io/Util.java @@ -57,6 +57,7 @@ import java.io.Writer; import java.io.UnsupportedEncodingException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Enumeration; @@ -2013,8 +2014,14 @@ public static String guessMimeType(String sourceFile) throws IOException { */ public static String guessMimeType(InputStream in) throws IOException { byte[] header = new byte[11]; - in.read(header, 0, 11); - return guessMimeType(header); + int bytesRead = in.read(header, 0, header.length); + if (bytesRead <= 0) { + return "application/octet-stream"; + } + if (bytesRead == header.length) { + return guessMimeType(header); + } + return guessMimeType(Arrays.copyOf(header, bytesRead)); } /** diff --git a/CodenameOne/src/com/codename1/io/gzip/Deflate.java b/CodenameOne/src/com/codename1/io/gzip/Deflate.java index 627495b644..16ea3a9b89 100644 --- a/CodenameOne/src/com/codename1/io/gzip/Deflate.java +++ b/CodenameOne/src/com/codename1/io/gzip/Deflate.java @@ -46,8 +46,8 @@ final class Deflate { static final private int STORED = 0; static final private int FAST = 1; static final private int SLOW = 2; - static final private Config[] config_table; - static final private String[] z_errmsg = { + static final private Config[] CONFIG_TABLE; + static final private String[] Z_ERRMSG = { "need dictionary", // Z_NEED_DICT 2 "stream end", // Z_STREAM_END 1 "", // Z_OK 0 @@ -60,13 +60,13 @@ final class Deflate { "" }; // block not completed, need more input or more output - static final private int NeedMore = 0; + static final private int NEED_MORE = 0; // block flush performed - static final private int BlockDone = 1; + static final private int BLOCK_DONE = 1; // finish started, need only more output at next deflate - static final private int FinishStarted = 2; + static final private int FINISH_STARTED = 2; // finish done, accept no more input or output - static final private int FinishDone = 3; + static final private int FINISH_DONE = 3; // preset dictionary flag in zlib header static final private int PRESET_DICT = 0x20; static final private int Z_FILTERED = 1; @@ -98,7 +98,7 @@ final class Deflate { static final private int Z_BINARY = 0; static final private int Z_ASCII = 1; static final private int Z_UNKNOWN = 2; - static final private int Buf_size = 8 * 2; + static final private int BUF_SIZE = 8 * 2; // repeat previous bit length 3-6 times (2 bits of repeat count) static final private int REP_3_6 = 16; // repeat a zero length 3-10 times (3 bits of repeat count) @@ -118,35 +118,35 @@ final class Deflate { static final private int END_BLOCK = 256; static { - config_table = new Config[10]; + CONFIG_TABLE = new Config[10]; // good lazy nice chain - config_table[0] = new Config(0, 0, 0, 0, STORED); - config_table[1] = new Config(4, 4, 8, 4, FAST); - config_table[2] = new Config(4, 5, 16, 8, FAST); - config_table[3] = new Config(4, 6, 32, 32, FAST); - - config_table[4] = new Config(4, 4, 16, 16, SLOW); - config_table[5] = new Config(8, 16, 32, 32, SLOW); - config_table[6] = new Config(8, 16, 128, 128, SLOW); - config_table[7] = new Config(8, 32, 128, 256, SLOW); - config_table[8] = new Config(32, 128, 258, 1024, SLOW); - config_table[9] = new Config(32, 258, 258, 4096, SLOW); + CONFIG_TABLE[0] = new Config(0, 0, 0, 0, STORED); + CONFIG_TABLE[1] = new Config(4, 4, 8, 4, FAST); + CONFIG_TABLE[2] = new Config(4, 5, 16, 8, FAST); + CONFIG_TABLE[3] = new Config(4, 6, 32, 32, FAST); + + CONFIG_TABLE[4] = new Config(4, 4, 16, 16, SLOW); + CONFIG_TABLE[5] = new Config(8, 16, 32, 32, SLOW); + CONFIG_TABLE[6] = new Config(8, 16, 128, 128, SLOW); + CONFIG_TABLE[7] = new Config(8, 32, 128, 256, SLOW); + CONFIG_TABLE[8] = new Config(32, 128, 258, 1024, SLOW); + CONFIG_TABLE[9] = new Config(32, 258, 258, 4096, SLOW); } ZStream strm; // pointer back to this zlib stream int status; // as the name implies - byte[] pending_buf; // output still pending - int pending_buf_size; // size of pending_buf - int pending_out; // next pending byte to output to the stream + byte[] pendingBuf; // output still pending + int pendingBufSize; // size of pending_buf + int pendingOut; // next pending byte to output to the stream int pending; // nb of bytes in the pending buffer int wrap = 1; - byte data_type; // UNKNOWN, BINARY or ASCII - int last_flush; // value of flush param for previous deflate call - int w_size; // LZ77 window size (32K by default) - int w_bits; // log2(w_size) (8..16) - int w_mask; // w_size - 1 + byte dataType; // UNKNOWN, BINARY or ASCII + int lastFlush; // value of flush param for previous deflate call + int wSize; // LZ77 window size (32K by default) + int wBits; // log2(w_size) (8..16) + int wMask; // w_size - 1 byte[] window; - int window_size; + int windowSize; // Sliding window. Input bytes are read into the second half of the window, // and move to the first half later to keep a dictionary of at least wSize // bytes. With this organization, matches are limited to a distance of @@ -161,35 +161,35 @@ final class Deflate { // Link to older string with same hash index. To limit the size of this // array to 64K, this link is maintained only for the last 32K strings. // An index in this array is thus a window index modulo 32K. - int ins_h; // hash index of string to be inserted - int hash_size; // number of elements in hash table - int hash_bits; // log2(hash_size) - int hash_mask; // hash_size-1 + int insH; // hash index of string to be inserted + int hashSize; // number of elements in hash table + int hashBits; // log2(hash_size) + int hashMask; // hash_size-1 // Number of bits by which ins_h must be shifted at each input // step. It must be such that after MIN_MATCH steps, the oldest // byte no longer takes part in the hash key, that is: // hash_shift * MIN_MATCH >= hash_bits - int hash_shift; - int block_start; + int hashShift; + int blockStart; // Window position at the beginning of the current output block. Gets // negative when the window is moved backwards. - int match_length; // length of best match - int prev_match; // previous match - int match_available; // set if previous match exists - int strstart; // start of string to insert - int match_start; // start of matching string + int matchLength; // length of best match + int prevMatch; // previous match + int matchAvailable; // set if previous match exists + int strStart; // start of string to insert + int matchStart; // start of matching string int lookahead; // number of valid bytes ahead in window // Length of the best match at previous step. Matches not greater than this // are discarded. This is used in the lazy match evaluation. - int prev_length; + int prevLength; // To speed up deflation, hash chains are never searched beyond this // length. A higher limit improves compression ratio but degrades the speed. - int max_chain_length; + int maxChainLength; // Attempt to find a better match only when the current match is strictly // smaller than this value. This mechanism is used only for compression // levels >= 4. - int max_lazy_match; + int maxLazyMatch; int level; // compression level (1..9) // Insert new strings in the hash table only if the match length is not @@ -197,28 +197,28 @@ final class Deflate { // max_insert_length is used only for compression levels <= 3. int strategy; // favor or force Huffman coding // Use a faster search when the previous match is longer than this - int good_match; + int goodMatch; // Stop searching when current match exceeds this - int nice_match; - short[] dyn_ltree; // literal and length tree - short[] dyn_dtree; // distance tree - short[] bl_tree; // Huffman tree for bit lengths - Tree l_desc = new Tree(); // desc for literal tree - Tree d_desc = new Tree(); // desc for distance tree - Tree bl_desc = new Tree(); // desc for bit length tree + int niceMatch; + short[] dynLtree; // literal and length tree + short[] dynDtree; // distance tree + short[] blTree; // Huffman tree for bit lengths + Tree lDesc = new Tree(); // desc for literal tree + Tree dDesc = new Tree(); // desc for distance tree + Tree blDesc = new Tree(); // desc for bit length tree // number of codes at each bit length for an optimal tree - short[] bl_count = new short[MAX_BITS + 1]; + short[] blCount = new short[MAX_BITS + 1]; // working area to be used in Tree#gen_codes() - short[] next_code = new short[MAX_BITS + 1]; + short[] nextCode = new short[MAX_BITS + 1]; // heap used to build the Huffman trees int[] heap = new int[2 * L_CODES + 1]; - int heap_len; // number of elements in the heap - int heap_max; // element of largest frequency + int heapLen; // number of elements in the heap + int heapMax; // element of largest frequency // Depth of each subtree used as tie breaker for trees of equal frequency byte[] depth = new byte[2 * L_CODES + 1]; // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. // The same heap array is used to build all trees. - int l_buf; // index for literals or lengths */ + int lBuf; // index for literals or lengths */ // Size of match buffer for literals/lengths. There are 4 reasons for // limiting lit_bufsize to 64K: // - frequencies can be kept in 16 bit counters @@ -236,30 +236,30 @@ final class Deflate { // fast adaptation but have of course the overhead of transmitting // trees more frequently. // - I can't count above 4 - int lit_bufsize; - int last_lit; // running index in l_buf - int d_buf; // index of pendig_buf + int litBufSize; + int lastLit; // running index in l_buf + int dBuf; // index of pendig_buf // Buffer for distances. To simplify the code, d_buf and l_buf have // the same number of elements. To use different lengths, an extra flag // array would be necessary. - int opt_len; // bit length of current block with optimal trees - int static_len; // bit length of current block with static trees + int optLen; // bit length of current block with optimal trees + int staticLen; // bit length of current block with static trees int matches; // number of string matches in current block - int last_eob_len; // bit length of EOB code for last block + int lastEobLen; // bit length of EOB code for last block // Output buffer. bits are inserted starting at the bottom (least // significant bits). - short bi_buf; + short biBuf; // Number of valid bits in bi_buf. All bits above the last valid bit // are always zero. - int bi_valid; + int biValid; GZIPHeader gheader = null; Deflate(ZStream strm) { this.strm = strm; - dyn_ltree = new short[HEAP_SIZE * 2]; - dyn_dtree = new short[(2 * D_CODES + 1) * 2]; // distance tree - bl_tree = new short[(2 * BL_CODES + 1) * 2]; // Huffman tree for bit lengths + dynLtree = new short[HEAP_SIZE * 2]; + dynDtree = new short[(2 * D_CODES + 1) * 2]; // distance tree + blTree = new short[(2 * BL_CODES + 1) * 2]; // Huffman tree for bit lengths } static boolean smaller(short[] tree, int n, int m, byte[] depth) { @@ -275,25 +275,25 @@ static int deflateCopy(ZStream dest, ZStream src) { return Z_STREAM_ERROR; } - if (src.next_in != null) { - dest.next_in = new byte[src.next_in.length]; - System.arraycopy(src.next_in, 0, dest.next_in, 0, src.next_in.length); + if (src.nextIn != null) { + dest.nextIn = new byte[src.nextIn.length]; + System.arraycopy(src.nextIn, 0, dest.nextIn, 0, src.nextIn.length); } - dest.next_in_index = src.next_in_index; - dest.avail_in = src.avail_in; - dest.total_in = src.total_in; + dest.nextInIndex = src.nextInIndex; + dest.availIn = src.availIn; + dest.totalIn = src.totalIn; - if (src.next_out != null) { - dest.next_out = new byte[src.next_out.length]; - System.arraycopy(src.next_out, 0, dest.next_out, 0, src.next_out.length); + if (src.nextOut != null) { + dest.nextOut = new byte[src.nextOut.length]; + System.arraycopy(src.nextOut, 0, dest.nextOut, 0, src.nextOut.length); } - dest.next_out_index = src.next_out_index; - dest.avail_out = src.avail_out; - dest.total_out = src.total_out; + dest.nextOutIndex = src.nextOutIndex; + dest.availOut = src.availOut; + dest.totalOut = src.totalOut; dest.msg = src.msg; - dest.data_type = src.data_type; + dest.dataType = src.dataType; dest.adler = src.adler.copy(); dest.dstate = (Deflate) src.dstate.clone(); @@ -301,57 +301,57 @@ static int deflateCopy(ZStream dest, ZStream src) { return Z_OK; } - void lm_init() { - window_size = 2 * w_size; + void lmInit() { + windowSize = 2 * wSize; - head[hash_size - 1] = 0; - for (int i = 0; i < hash_size - 1; i++) { + head[hashSize - 1] = 0; + for (int i = 0; i < hashSize - 1; i++) { head[i] = 0; } // Set the default configuration parameters: - max_lazy_match = Deflate.config_table[level].max_lazy; - good_match = Deflate.config_table[level].good_length; - nice_match = Deflate.config_table[level].nice_length; - max_chain_length = Deflate.config_table[level].max_chain; + maxLazyMatch = Deflate.CONFIG_TABLE[level].maxLazy; + goodMatch = Deflate.CONFIG_TABLE[level].goodLength; + niceMatch = Deflate.CONFIG_TABLE[level].niceLength; + maxChainLength = Deflate.CONFIG_TABLE[level].maxChain; - strstart = 0; - block_start = 0; + strStart = 0; + blockStart = 0; lookahead = 0; - match_length = prev_length = MIN_MATCH - 1; - match_available = 0; - ins_h = 0; + matchLength = prevLength = MIN_MATCH - 1; + matchAvailable = 0; + insH = 0; } // Initialize the tree data structures for a new zlib stream. - void tr_init() { + void trInit() { - l_desc.dyn_tree = dyn_ltree; - l_desc.stat_desc = StaticTree.static_l_desc; + lDesc.dynTree = dynLtree; + lDesc.statDesc = StaticTree.staticLDesc; - d_desc.dyn_tree = dyn_dtree; - d_desc.stat_desc = StaticTree.static_d_desc; + dDesc.dynTree = dynDtree; + dDesc.statDesc = StaticTree.staticDDesc; - bl_desc.dyn_tree = bl_tree; - bl_desc.stat_desc = StaticTree.static_bl_desc; + blDesc.dynTree = blTree; + blDesc.statDesc = StaticTree.staticBlDesc; - bi_buf = 0; - bi_valid = 0; - last_eob_len = 8; // enough lookahead for inflate + biBuf = 0; + biValid = 0; + lastEobLen = 8; // enough lookahead for inflate // Initialize the first block of the first file: - init_block(); + initBlock(); } - void init_block() { + void initBlock() { // Initialize the trees. - for (int i = 0; i < L_CODES; i++) dyn_ltree[i * 2] = 0; - for (int i = 0; i < D_CODES; i++) dyn_dtree[i * 2] = 0; - for (int i = 0; i < BL_CODES; i++) bl_tree[i * 2] = 0; + for (int i = 0; i < L_CODES; i++) dynLtree[i * 2] = 0; + for (int i = 0; i < D_CODES; i++) dynDtree[i * 2] = 0; + for (int i = 0; i < BL_CODES; i++) blTree[i * 2] = 0; - dyn_ltree[END_BLOCK * 2] = 1; - opt_len = static_len = 0; - last_lit = matches = 0; + dynLtree[END_BLOCK * 2] = 1; + optLen = staticLen = 0; + lastLit = matches = 0; } // Restore the heap property by moving down the tree starting at node k, @@ -363,9 +363,9 @@ void pqdownheap(short[] tree, // the tree to restore ) { int v = heap[k]; int j = k << 1; // left son of k - while (j <= heap_len) { + while (j <= heapLen) { // Set j to the smallest of the two sons: - if (j < heap_len && + if (j < heapLen && smaller(tree, heap[j + 1], heap[j], depth)) { j++; } @@ -383,191 +383,191 @@ void pqdownheap(short[] tree, // the tree to restore // Scan a literal or distance tree to determine the frequencies of the codes // in the bit length tree. - void scan_tree(short[] tree,// the tree to be scanned - int max_code // and its largest code of non zero frequency + void scanTree(short[] tree,// the tree to be scanned + int maxCode // and its largest code of non-zero frequency ) { int n; // iterates over all tree elements int prevlen = -1; // last emitted length int curlen; // length of current code int nextlen = tree[1]; // length of next code int count = 0; // repeat count of the current code - int max_count = 7; // max repeat count - int min_count = 4; // min repeat count + int maxCount = 7; // max repeat count + int minCount = 4; // min repeat count if (nextlen == 0) { - max_count = 138; - min_count = 3; + maxCount = 138; + minCount = 3; } - tree[(max_code + 1) * 2 + 1] = (short) 0xffff; // guard + tree[(maxCode + 1) * 2 + 1] = (short) 0xffff; // guard - for (n = 0; n <= max_code; n++) { + for (n = 0; n <= maxCode; n++) { curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]; - if (++count < max_count && curlen == nextlen) { + if (++count < maxCount && curlen == nextlen) { continue; - } else if (count < min_count) { - bl_tree[curlen * 2] += count; + } else if (count < minCount) { + blTree[curlen * 2] += count; } else if (curlen != 0) { - if (curlen != prevlen) bl_tree[curlen * 2]++; - bl_tree[REP_3_6 * 2]++; + if (curlen != prevlen) blTree[curlen * 2]++; + blTree[REP_3_6 * 2]++; } else if (count <= 10) { - bl_tree[REPZ_3_10 * 2]++; + blTree[REPZ_3_10 * 2]++; } else { - bl_tree[REPZ_11_138 * 2]++; + blTree[REPZ_11_138 * 2]++; } count = 0; prevlen = curlen; if (nextlen == 0) { - max_count = 138; - min_count = 3; + maxCount = 138; + minCount = 3; } else if (curlen == nextlen) { - max_count = 6; - min_count = 3; + maxCount = 6; + minCount = 3; } else { - max_count = 7; - min_count = 4; + maxCount = 7; + minCount = 4; } } } // Construct the Huffman tree for the bit lengths and return the index in - // bl_order of the last bit length code to send. - int build_bl_tree() { - int max_blindex; // index of last bit length code of non zero freq + // BL_ORDER of the last bit length code to send. + int buildBlTree() { + int maxBlindex; // index of last bit length code of non zero freq // Determine the bit length frequencies for literal and distance trees - scan_tree(dyn_ltree, l_desc.max_code); - scan_tree(dyn_dtree, d_desc.max_code); + scanTree(dynLtree, lDesc.maxCode); + scanTree(dynDtree, dDesc.maxCode); // Build the bit length tree: - bl_desc.build_tree(this); + blDesc.buildTree(this); // opt_len now includes the length of the tree representations, except // the lengths of the bit lengths codes and the 5+5+4 bits for the counts. // Determine the number of bit length codes to send. The pkzip format // requires that at least 4 bit length codes be sent. (appnote.txt says // 3 but the actual value used is 4.) - for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) { - if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] != 0) break; + for (maxBlindex = BL_CODES - 1; maxBlindex >= 3; maxBlindex--) { + if (blTree[Tree.BL_ORDER[maxBlindex] * 2 + 1] != 0) break; } // Update opt_len to include the bit length tree and counts - opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; + optLen += 3 * (maxBlindex + 1) + 5 + 5 + 4; - return max_blindex; + return maxBlindex; } // Send the header for a block using dynamic Huffman trees: the counts, the // lengths of the bit length codes, the literal tree and the distance tree. // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. - void send_all_trees(int lcodes, int dcodes, int blcodes) { + void sendAllTrees(int lcodes, int dcodes, int blcodes) { int rank; // index in bl_order - send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt - send_bits(dcodes - 1, 5); - send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt + sendBits(lcodes - 257, 5); // not +255 as stated in appnote.txt + sendBits(dcodes - 1, 5); + sendBits(blcodes - 4, 4); // not -3 as stated in appnote.txt for (rank = 0; rank < blcodes; rank++) { - send_bits(bl_tree[Tree.bl_order[rank] * 2 + 1], 3); + sendBits(blTree[Tree.BL_ORDER[rank] * 2 + 1], 3); } - send_tree(dyn_ltree, lcodes - 1); // literal tree - send_tree(dyn_dtree, dcodes - 1); // distance tree + sendTree(dynLtree, lcodes - 1); // literal tree + sendTree(dynDtree, dcodes - 1); // distance tree } // Send a literal or distance tree in compressed form, using the codes in // bl_tree. - void send_tree(short[] tree,// the tree to be sent - int max_code // and its largest code of non zero frequency + void sendTree(short[] tree,// the tree to be sent + int maxCode // and its largest code of non zero frequency ) { int n; // iterates over all tree elements int prevlen = -1; // last emitted length int curlen; // length of current code int nextlen = tree[1]; // length of next code int count = 0; // repeat count of the current code - int max_count = 7; // max repeat count - int min_count = 4; // min repeat count + int maxCount = 7; // max repeat count + int minCount = 4; // min repeat count if (nextlen == 0) { - max_count = 138; - min_count = 3; + maxCount = 138; + minCount = 3; } - for (n = 0; n <= max_code; n++) { + for (n = 0; n <= maxCode; n++) { curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]; - if (++count < max_count && curlen == nextlen) { + if (++count < maxCount && curlen == nextlen) { continue; - } else if (count < min_count) { + } else if (count < minCount) { do { - send_code(curlen, bl_tree); + sendCode(curlen, blTree); } while (--count != 0); } else if (curlen != 0) { if (curlen != prevlen) { - send_code(curlen, bl_tree); + sendCode(curlen, blTree); count--; } - send_code(REP_3_6, bl_tree); - send_bits(count - 3, 2); + sendCode(REP_3_6, blTree); + sendBits(count - 3, 2); } else if (count <= 10) { - send_code(REPZ_3_10, bl_tree); - send_bits(count - 3, 3); + sendCode(REPZ_3_10, blTree); + sendBits(count - 3, 3); } else { - send_code(REPZ_11_138, bl_tree); - send_bits(count - 11, 7); + sendCode(REPZ_11_138, blTree); + sendBits(count - 11, 7); } count = 0; prevlen = curlen; if (nextlen == 0) { - max_count = 138; - min_count = 3; + maxCount = 138; + minCount = 3; } else if (curlen == nextlen) { - max_count = 6; - min_count = 3; + maxCount = 6; + minCount = 3; } else { - max_count = 7; - min_count = 4; + maxCount = 7; + minCount = 4; } } } // Output a byte on the stream. // IN assertion: there is enough room in pending_buf. - void put_byte(byte[] p, int start, int len) { - System.arraycopy(p, start, pending_buf, pending, len); + void putByte(byte[] p, int start, int len) { + System.arraycopy(p, start, pendingBuf, pending, len); pending += len; } - void put_byte(byte c) { - pending_buf[pending++] = c; + void putByte(byte c) { + pendingBuf[pending++] = c; } - void put_short(int w) { - put_byte((byte) (w/*&0xff*/)); - put_byte((byte) (w >>> 8)); + void putShort(int w) { + putByte((byte) (w/*&0xff*/)); + putByte((byte) (w >>> 8)); } void putShortMSB(int b) { - put_byte((byte) (b >> 8)); - put_byte((byte) (b/*&0xff*/)); + putByte((byte) (b >> 8)); + putByte((byte) (b/*&0xff*/)); } - void send_code(int c, short[] tree) { + void sendCode(int c, short[] tree) { int c2 = c * 2; - send_bits((tree[c2] & 0xffff), (tree[c2 + 1] & 0xffff)); + sendBits((tree[c2] & 0xffff), (tree[c2 + 1] & 0xffff)); } - void send_bits(int value, int length) { + void sendBits(int value, int length) { int len = length; - if (bi_valid > Buf_size - len) { + if (biValid > BUF_SIZE - len) { int val = value; // bi_buf |= (val << bi_valid); - bi_buf |= ((val << bi_valid) & 0xffff); - put_short(bi_buf); - bi_buf = (short) (val >>> (Buf_size - bi_valid)); - bi_valid += len - Buf_size; + biBuf |= ((val << biValid) & 0xffff); + putShort(biBuf); + biBuf = (short) (val >>> (BUF_SIZE - biValid)); + biValid += len - BUF_SIZE; } else { // bi_buf |= (value) << bi_valid; - bi_buf |= (((value) << bi_valid) & 0xffff); - bi_valid += len; + biBuf |= (((value) << biValid) & 0xffff); + biValid += len; } } @@ -580,189 +580,189 @@ void send_bits(int value, int length) { // of one. (There are no problems if the previous block is stored or fixed.) // To simplify the code, we assume the worst case of last real code encoded // on one bit only. - void _tr_align() { - send_bits(STATIC_TREES << 1, 3); - send_code(END_BLOCK, StaticTree.static_ltree); + void trAlign() { + sendBits(STATIC_TREES << 1, 3); + sendCode(END_BLOCK, StaticTree.STATIC_LTREE); - bi_flush(); + biFlush(); // Of the 10 bits for the empty block, we have already sent // (10 - bi_valid) bits. The lookahead for the last real code (before // the EOB of the previous block) was thus at least one plus the length // of the EOB plus what we have just sent of the empty static block. - if (1 + last_eob_len + 10 - bi_valid < 9) { - send_bits(STATIC_TREES << 1, 3); - send_code(END_BLOCK, StaticTree.static_ltree); - bi_flush(); + if (1 + lastEobLen + 10 - biValid < 9) { + sendBits(STATIC_TREES << 1, 3); + sendCode(END_BLOCK, StaticTree.STATIC_LTREE); + biFlush(); } - last_eob_len = 7; + lastEobLen = 7; } // Save the match info and tally the frequency counts. Return true if // the current block must be flushed. - boolean _tr_tally(int dist, // distance of matched string - int lc // match length-MIN_MATCH or unmatched char (if dist==0) + boolean trTally(int dist, // distance of matched string + int lc // match length-MIN_MATCH or unmatched char (if dist==0) ) { - pending_buf[d_buf + last_lit * 2] = (byte) (dist >>> 8); - pending_buf[d_buf + last_lit * 2 + 1] = (byte) dist; + pendingBuf[dBuf + lastLit * 2] = (byte) (dist >>> 8); + pendingBuf[dBuf + lastLit * 2 + 1] = (byte) dist; - pending_buf[l_buf + last_lit] = (byte) lc; - last_lit++; + pendingBuf[lBuf + lastLit] = (byte) lc; + lastLit++; if (dist == 0) { // lc is the unmatched char - dyn_ltree[lc * 2]++; + dynLtree[lc * 2]++; } else { matches++; // Here, lc is the match length - MIN_MATCH dist--; // dist = match distance - 1 - dyn_ltree[(Tree._length_code[lc] + LITERALS + 1) * 2]++; - dyn_dtree[Tree.d_code(dist) * 2]++; + dynLtree[(Tree.LENGTH_CODE[lc] + LITERALS + 1) * 2]++; + dynDtree[Tree.dCode(dist) * 2]++; } - if ((last_lit & 0x1fff) == 0 && level > 2) { + if ((lastLit & 0x1fff) == 0 && level > 2) { // Compute an upper bound for the compressed length - int out_length = last_lit * 8; - int in_length = strstart - block_start; + int outLength = lastLit * 8; + int inLength = strStart - blockStart; int dcode; for (dcode = 0; dcode < D_CODES; dcode++) { - out_length += (int) dyn_dtree[dcode * 2] * - (5L + Tree.extra_dbits[dcode]); + outLength += (int) dynDtree[dcode * 2] * + (5L + Tree.EXTRA_DBITS[dcode]); } - out_length >>>= 3; - if ((matches < (last_lit / 2)) && out_length < in_length / 2) return true; + outLength >>>= 3; + if ((matches < (lastLit / 2)) && outLength < inLength / 2) return true; } - return (last_lit == lit_bufsize - 1); + return (lastLit == litBufSize - 1); // We avoid equality with lit_bufsize because of wraparound at 64K // on 16 bit machines and because stored blocks are restricted to // 64K-1 bytes. } // Send the block data compressed using the given Huffman trees - void compress_block(short[] ltree, short[] dtree) { + void compressBlock(short[] ltree, short[] dtree) { int dist; // distance of matched string int lc; // match length or unmatched char (if dist == 0) int lx = 0; // running index in l_buf int code; // the code to send int extra; // number of extra bits to send - if (last_lit != 0) { + if (lastLit != 0) { do { - dist = ((pending_buf[d_buf + lx * 2] << 8) & 0xff00) | - (pending_buf[d_buf + lx * 2 + 1] & 0xff); - lc = (pending_buf[l_buf + lx]) & 0xff; + dist = ((pendingBuf[dBuf + lx * 2] << 8) & 0xff00) | + (pendingBuf[dBuf + lx * 2 + 1] & 0xff); + lc = (pendingBuf[lBuf + lx]) & 0xff; lx++; if (dist == 0) { - send_code(lc, ltree); // send a literal byte + sendCode(lc, ltree); // send a literal byte } else { // Here, lc is the match length - MIN_MATCH - code = Tree._length_code[lc]; + code = Tree.LENGTH_CODE[lc]; - send_code(code + LITERALS + 1, ltree); // send the length code - extra = Tree.extra_lbits[code]; + sendCode(code + LITERALS + 1, ltree); // send the length code + extra = Tree.EXTRA_LBITS[code]; if (extra != 0) { - lc -= Tree.base_length[code]; - send_bits(lc, extra); // send the extra length bits + lc -= Tree.BASE_LENGTH[code]; + sendBits(lc, extra); // send the extra length bits } dist--; // dist is now the match distance - 1 - code = Tree.d_code(dist); + code = Tree.dCode(dist); - send_code(code, dtree); // send the distance code - extra = Tree.extra_dbits[code]; + sendCode(code, dtree); // send the distance code + extra = Tree.EXTRA_DBITS[code]; if (extra != 0) { - dist -= Tree.base_dist[code]; - send_bits(dist, extra); // send the extra distance bits + dist -= Tree.BASE_DIST[code]; + sendBits(dist, extra); // send the extra distance bits } } // literal or match pair ? // Check that the overlay between pending_buf and d_buf+l_buf is ok: } - while (lx < last_lit); + while (lx < lastLit); } - send_code(END_BLOCK, ltree); - last_eob_len = ltree[END_BLOCK * 2 + 1]; + sendCode(END_BLOCK, ltree); + lastEobLen = ltree[END_BLOCK * 2 + 1]; } // Set the data type to ASCII or BINARY, using a crude approximation: // binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise. // IN assertion: the fields freq of dyn_ltree are set and the total of all // frequencies does not exceed 64K (to fit in an int on 16 bit machines). - void set_data_type() { + void setDataType() { int n = 0; - int ascii_freq = 0; - int bin_freq = 0; + int asciiFreq = 0; + int binFreq = 0; while (n < 7) { - bin_freq += dyn_ltree[n * 2]; + binFreq += dynLtree[n * 2]; n++; } while (n < 128) { - ascii_freq += dyn_ltree[n * 2]; + asciiFreq += dynLtree[n * 2]; n++; } while (n < LITERALS) { - bin_freq += dyn_ltree[n * 2]; + binFreq += dynLtree[n * 2]; n++; } - data_type = (byte) (bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII); + dataType = (byte) (binFreq > (asciiFreq >>> 2) ? Z_BINARY : Z_ASCII); } // Flush the bit buffer, keeping at most 7 bits in it. - void bi_flush() { - if (bi_valid == 16) { - put_short(bi_buf); - bi_buf = 0; - bi_valid = 0; - } else if (bi_valid >= 8) { - put_byte((byte) bi_buf); - bi_buf >>>= 8; - bi_valid -= 8; + void biFlush() { + if (biValid == 16) { + putShort(biBuf); + biBuf = 0; + biValid = 0; + } else if (biValid >= 8) { + putByte((byte) biBuf); + biBuf >>>= 8; + biValid -= 8; } } // Flush the bit buffer and align the output on a byte boundary - void bi_windup() { - if (bi_valid > 8) { - put_short(bi_buf); - } else if (bi_valid > 0) { - put_byte((byte) bi_buf); + void biWindup() { + if (biValid > 8) { + putShort(biBuf); + } else if (biValid > 0) { + putByte((byte) biBuf); } - bi_buf = 0; - bi_valid = 0; + biBuf = 0; + biValid = 0; } // Copy a stored block, storing first the length and its // one's complement if requested. - void copy_block(int buf, // the input data - int len, // its length - boolean header // true if block header must be written + void copyBlock(int buf, // the input data + int len, // its length + boolean header // true if block header must be written ) { int index = 0; - bi_windup(); // align on byte boundary - last_eob_len = 8; // enough lookahead for inflate + biWindup(); // align on byte boundary + lastEobLen = 8; // enough lookahead for inflate if (header) { - put_short((short) len); - put_short((short) ~len); + putShort((short) len); + putShort((short) ~len); } // while(len--!=0) { // put_byte(window[buf+index]); // index++; // } - put_byte(window, buf, len); + putByte(window, buf, len); } - void flush_block_only(boolean eof) { - _tr_flush_block(block_start >= 0 ? block_start : -1, - strstart - block_start, + void flushBlockOnly(boolean eof) { + trFlushBlock(blockStart >= 0 ? blockStart : -1, + strStart - blockStart, eof); - block_start = strstart; - strm.flush_pending(); + blockStart = strStart; + strm.flushPending(); } // Copy without compression as much as possible from the input stream, return @@ -772,124 +772,124 @@ void flush_block_only(boolean eof) { // only for the level=0 compression option. // NOTE: this function should be optimized to avoid extra copying from // window to pending_buf. - int deflate_stored(int flush) { + int deflateStored(int flush) { // Stored blocks are limited to 0xffff bytes, pending_buf is limited // to pending_buf_size, and each stored block has a 5 byte header: - int max_block_size = 0xffff; - int max_start; + int maxBlockSize = 0xffff; + int maxStart; - if (max_block_size > pending_buf_size - 5) { - max_block_size = pending_buf_size - 5; + if (maxBlockSize > pendingBufSize - 5) { + maxBlockSize = pendingBufSize - 5; } // Copy as much as possible from input to output: while (true) { // Fill the window as much as possible: if (lookahead <= 1) { - fill_window(); - if (lookahead == 0 && flush == Z_NO_FLUSH) return NeedMore; + fillWindow(); + if (lookahead == 0 && flush == Z_NO_FLUSH) return NEED_MORE; if (lookahead == 0) break; // flush the current block } - strstart += lookahead; + strStart += lookahead; lookahead = 0; // Emit a stored block if pending_buf will be full: - max_start = block_start + max_block_size; - if (strstart == 0 || strstart >= max_start) { + maxStart = blockStart + maxBlockSize; + if (strStart == 0 || strStart >= maxStart) { // strstart == 0 is possible when wraparound on 16-bit machine - lookahead = strstart - max_start; - strstart = max_start; + lookahead = strStart - maxStart; + strStart = maxStart; - flush_block_only(false); - if (strm.avail_out == 0) return NeedMore; + flushBlockOnly(false); + if (strm.availOut == 0) return NEED_MORE; } // Flush if we may have to slide, otherwise block_start may become // negative and the data will be gone: - if (strstart - block_start >= w_size - MIN_LOOKAHEAD) { - flush_block_only(false); - if (strm.avail_out == 0) return NeedMore; + if (strStart - blockStart >= wSize - MIN_LOOKAHEAD) { + flushBlockOnly(false); + if (strm.availOut == 0) return NEED_MORE; } } - flush_block_only(flush == Z_FINISH); - if (strm.avail_out == 0) - return (flush == Z_FINISH) ? FinishStarted : NeedMore; + flushBlockOnly(flush == Z_FINISH); + if (strm.availOut == 0) + return (flush == Z_FINISH) ? FINISH_STARTED : NEED_MORE; - return flush == Z_FINISH ? FinishDone : BlockDone; + return flush == Z_FINISH ? FINISH_DONE : BLOCK_DONE; } // Send a stored block - void _tr_stored_block(int buf, // input block - int stored_len, // length of input block - boolean eof // true if this is the last block for a file + void trStoredBlock(int buf, // input block + int storedLen, // length of input block + boolean eof // true if this is the last block for a file ) { - send_bits((eof ? 1 : 0), 3); // send block type - copy_block(buf, stored_len, true); // with header + sendBits((eof ? 1 : 0), 3); // send block type + copyBlock(buf, storedLen, true); // with header } // Determine the best encoding for the current block: dynamic trees, static // trees or store, and output the encoded block to the zip file. - void _tr_flush_block(int buf, // input block, or NULL if too old - int stored_len, // length of input block - boolean eof // true if this is the last block for a file + void trFlushBlock(int buf, // input block, or NULL if too old + int storedLen, // length of input block + boolean eof // true if this is the last block for a file ) { - int opt_lenb, static_lenb;// opt_len and static_len in bytes - int max_blindex = 0; // index of last bit length code of non zero freq + int optLenb, staticLenb;// opt_len and static_len in bytes + int maxBlIndex = 0; // index of last bit length code of non zero freq // Build the Huffman trees unless a stored block is forced if (level > 0) { // Check if the file is ascii or binary - if (data_type == Z_UNKNOWN) set_data_type(); + if (dataType == Z_UNKNOWN) setDataType(); // Construct the literal and distance trees - l_desc.build_tree(this); + lDesc.buildTree(this); - d_desc.build_tree(this); + dDesc.buildTree(this); // At this point, opt_len and static_len are the total bit lengths of // the compressed block data, excluding the tree representations. // Build the bit length tree for the above two trees, and get the index - // in bl_order of the last bit length code to send. - max_blindex = build_bl_tree(); + // in BL_ORDER of the last bit length code to send. + maxBlIndex = buildBlTree(); // Determine the best encoding. Compute first the block length in bytes - opt_lenb = (opt_len + 3 + 7) >>> 3; - static_lenb = (static_len + 3 + 7) >>> 3; + optLenb = (optLen + 3 + 7) >>> 3; + staticLenb = (staticLen + 3 + 7) >>> 3; - if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + if (staticLenb <= optLenb) optLenb = staticLenb; } else { - opt_lenb = static_lenb = stored_len + 5; // force a stored block + optLenb = staticLenb = storedLen + 5; // force a stored block } - if (stored_len + 4 <= opt_lenb && buf != -1) { + if (storedLen + 4 <= optLenb && buf != -1) { // 4: two words for the lengths // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. // Otherwise we can't have processed more than WSIZE input bytes since // the last block flush, because compression would have been // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to // transform a block into a stored block. - _tr_stored_block(buf, stored_len, eof); - } else if (static_lenb == opt_lenb) { - send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3); - compress_block(StaticTree.static_ltree, StaticTree.static_dtree); + trStoredBlock(buf, storedLen, eof); + } else if (staticLenb == optLenb) { + sendBits((STATIC_TREES << 1) + (eof ? 1 : 0), 3); + compressBlock(StaticTree.STATIC_LTREE, StaticTree.STATIC_DTREE); } else { - send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3); - send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1); - compress_block(dyn_ltree, dyn_dtree); + sendBits((DYN_TREES << 1) + (eof ? 1 : 0), 3); + sendAllTrees(lDesc.maxCode + 1, dDesc.maxCode + 1, maxBlIndex + 1); + compressBlock(dynLtree, dynDtree); } // The above check is made mod 2^32, for files larger than 512 MB // and uLong implemented on 32 bits. - init_block(); + initBlock(); if (eof) { - bi_windup(); + biWindup(); } } @@ -901,17 +901,17 @@ void _tr_flush_block(int buf, // input block, or NULL if too old // At least one byte has been read, or avail_in == 0; reads are // performed for at least two bytes (required for the zip translate_eol // option -- not supported here). - void fill_window() { + void fillWindow() { int n, m; int p; int more; // Amount of free space at the end of the window. do { - more = (window_size - lookahead - strstart); + more = (windowSize - lookahead - strStart); // Deal with !@#$% 64K limit: - if (more == 0 && strstart == 0 && lookahead == 0) { - more = w_size; + if (more == 0 && strStart == 0 && lookahead == 0) { + more = wSize; } else if (more == -1) { // Very unlikely, but possible on 16 bit machine if strstart == 0 // and lookahead == 1 (input done one byte at time) @@ -919,11 +919,11 @@ void fill_window() { // If the window is almost full and there is insufficient lookahead, // move the upper half to the lower one to make room in the upper half. - } else if (strstart >= w_size + w_size - MIN_LOOKAHEAD) { - System.arraycopy(window, w_size, window, 0, w_size); - match_start -= w_size; - strstart -= w_size; // we now have strstart >= MAX_DIST - block_start -= w_size; + } else if (strStart >= wSize + wSize - MIN_LOOKAHEAD) { + System.arraycopy(window, wSize, window, 0, wSize); + matchStart -= wSize; + strStart -= wSize; // we now have strstart >= MAX_DIST + blockStart -= wSize; // Slide the hash table (could be avoided with 32 bit values // at the expense of memory usage). We slide even when level == 0 @@ -931,27 +931,27 @@ void fill_window() { // later. (Using level 0 permanently is not an optimal usage of // zlib, so we don't care about this pathological case.) - n = hash_size; + n = hashSize; p = n; do { m = (head[--p] & 0xffff); - head[p] = (m >= w_size ? (short) (m - w_size) : 0); + head[p] = (m >= wSize ? (short) (m - wSize) : 0); } while (--n != 0); - n = w_size; + n = wSize; p = n; do { m = (prev[--p] & 0xffff); - prev[p] = (m >= w_size ? (short) (m - w_size) : 0); + prev[p] = (m >= wSize ? (short) (m - wSize) : 0); // If n is not on any hash chain, prev[n] is garbage but // its value will never be used. } while (--n != 0); - more += w_size; + more += wSize; } - if (strm.avail_in == 0) return; + if (strm.availIn == 0) return; // If there was no sliding: // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && @@ -964,18 +964,18 @@ void fill_window() { // Otherwise, window_size == 2*WSIZE so more >= 2. // If there was sliding, more >= WSIZE. So in all cases, more >= 2. - n = strm.read_buf(window, strstart + lookahead, more); + n = strm.read_buf(window, strStart + lookahead, more); lookahead += n; // Initialize the hash value now that we have some input: if (lookahead >= MIN_MATCH) { - ins_h = window[strstart] & 0xff; - ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask; + insH = window[strStart] & 0xff; + insH = (((insH) << hashShift) ^ (window[strStart + 1] & 0xff)) & hashMask; } // If the whole input has less than MIN_MATCH bytes, ins_h is garbage, // but this is not important since only literal bytes will be emitted. } - while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0); + while (lookahead < MIN_LOOKAHEAD && strm.availIn != 0); } // Compress as much as possible from the input stream, return the current @@ -983,9 +983,9 @@ void fill_window() { // This function does not perform lazy evaluation of matches and inserts // new strings in the dictionary only for unmatched strings or for short // matches. It is used only for the fast compression options. - int deflate_fast(int flush) { -// short hash_head = 0; // head of the hash chain - int hash_head = 0; // head of the hash chain + int deflateFast(int flush) { +// short hashHead = 0; // head of the hash chain + int hashHead = 0; // head of the hash chain boolean bflush; // set if current block must be flushed while (true) { @@ -994,101 +994,101 @@ int deflate_fast(int flush) { // for the next match, plus MIN_MATCH bytes to insert the // string following the next match. if (lookahead < MIN_LOOKAHEAD) { - fill_window(); + fillWindow(); if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { - return NeedMore; + return NEED_MORE; } if (lookahead == 0) break; // flush the current block } // Insert the string window[strstart .. strstart+2] in the - // dictionary, and set hash_head to the head of the hash chain: + // dictionary, and set hashHead to the head of the hash chain: if (lookahead >= MIN_MATCH) { - ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; + insH = (((insH) << hashShift) ^ (window[(strStart) + (MIN_MATCH - 1)] & 0xff)) & hashMask; -// prev[strstart&w_mask]=hash_head=head[ins_h]; - hash_head = (head[ins_h] & 0xffff); - prev[strstart & w_mask] = head[ins_h]; - head[ins_h] = (short) strstart; +// prev[strstart&w_mask]=hashHead=head[ins_h]; + hashHead = (head[insH] & 0xffff); + prev[strStart & wMask] = head[insH]; + head[insH] = (short) strStart; } // Find the longest match, discarding those <= prev_length. // At this point we have always match_length < MIN_MATCH - if (hash_head != 0L && - ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD + if (hashHead != 0L && + ((strStart - hashHead) & 0xffff) <= wSize - MIN_LOOKAHEAD ) { // To simplify the code, we prevent matches with the string // of window index 0 (in particular we have to avoid a match // of the string with itself at the start of the input file). if (strategy != Z_HUFFMAN_ONLY) { - match_length = longest_match(hash_head); + matchLength = longestMatch(hashHead); } // longest_match() sets match_start } - if (match_length >= MIN_MATCH) { + if (matchLength >= MIN_MATCH) { // check_match(strstart, match_start, match_length); - bflush = _tr_tally(strstart - match_start, match_length - MIN_MATCH); + bflush = trTally(strStart - matchStart, matchLength - MIN_MATCH); - lookahead -= match_length; + lookahead -= matchLength; // Insert new strings in the hash table only if the match length // is not too large. This saves time but degrades compression. - if (match_length <= max_lazy_match && + if (matchLength <= maxLazyMatch && lookahead >= MIN_MATCH) { - match_length--; // string at strstart already in hash table + matchLength--; // string at strstart already in hash table do { - strstart++; + strStart++; - ins_h = ((ins_h << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; -// prev[strstart&w_mask]=hash_head=head[ins_h]; - hash_head = (head[ins_h] & 0xffff); - prev[strstart & w_mask] = head[ins_h]; - head[ins_h] = (short) strstart; + insH = ((insH << hashShift) ^ (window[(strStart) + (MIN_MATCH - 1)] & 0xff)) & hashMask; +// prev[strstart&w_mask]=hashHead=head[ins_h]; + hashHead = (head[insH] & 0xffff); + prev[strStart & wMask] = head[insH]; + head[insH] = (short) strStart; // strstart never exceeds WSIZE-MAX_MATCH, so there are // always MIN_MATCH bytes ahead. } - while (--match_length != 0); - strstart++; + while (--matchLength != 0); + strStart++; } else { - strstart += match_length; - match_length = 0; - ins_h = window[strstart] & 0xff; + strStart += matchLength; + matchLength = 0; + insH = window[strStart] & 0xff; - ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask; + insH = (((insH) << hashShift) ^ (window[strStart + 1] & 0xff)) & hashMask; // If lookahead < MIN_MATCH, ins_h is garbage, but it does not // matter since it will be recomputed at next deflate call. } } else { // No match, output a literal byte - bflush = _tr_tally(0, window[strstart] & 0xff); + bflush = trTally(0, window[strStart] & 0xff); lookahead--; - strstart++; + strStart++; } if (bflush) { - flush_block_only(false); - if (strm.avail_out == 0) return NeedMore; + flushBlockOnly(false); + if (strm.availOut == 0) return NEED_MORE; } } - flush_block_only(flush == Z_FINISH); - if (strm.avail_out == 0) { - if (flush == Z_FINISH) return FinishStarted; - else return NeedMore; + flushBlockOnly(flush == Z_FINISH); + if (strm.availOut == 0) { + if (flush == Z_FINISH) return FINISH_STARTED; + else return NEED_MORE; } - return flush == Z_FINISH ? FinishDone : BlockDone; + return flush == Z_FINISH ? FINISH_DONE : BLOCK_DONE; } // Same as above, but achieves better compression. We use a lazy // evaluation for matches: a match is finally adopted only if there is // no better match at the next window position. - int deflate_slow(int flush) { -// short hash_head = 0; // head of hash chain - int hash_head = 0; // head of hash chain + int deflateSlow(int flush) { +// short hashHead = 0; // head of hash chain + int hashHead = 0; // head of hash chain boolean bflush; // set if current block must be flushed // Process the input block. @@ -1099,165 +1099,165 @@ int deflate_slow(int flush) { // string following the next match. if (lookahead < MIN_LOOKAHEAD) { - fill_window(); + fillWindow(); if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { - return NeedMore; + return NEED_MORE; } if (lookahead == 0) break; // flush the current block } // Insert the string window[strstart .. strstart+2] in the - // dictionary, and set hash_head to the head of the hash chain: + // dictionary, and set hashHead to the head of the hash chain: if (lookahead >= MIN_MATCH) { - ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; -// prev[strstart&w_mask]=hash_head=head[ins_h]; - hash_head = (head[ins_h] & 0xffff); - prev[strstart & w_mask] = head[ins_h]; - head[ins_h] = (short) strstart; + insH = (((insH) << hashShift) ^ (window[(strStart) + (MIN_MATCH - 1)] & 0xff)) & hashMask; +// prev[strstart&w_mask]=hashHead=head[ins_h]; + hashHead = (head[insH] & 0xffff); + prev[strStart & wMask] = head[insH]; + head[insH] = (short) strStart; } // Find the longest match, discarding those <= prev_length. - prev_length = match_length; - prev_match = match_start; - match_length = MIN_MATCH - 1; + prevLength = matchLength; + prevMatch = matchStart; + matchLength = MIN_MATCH - 1; - if (hash_head != 0 && prev_length < max_lazy_match && - ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD + if (hashHead != 0 && prevLength < maxLazyMatch && + ((strStart - hashHead) & 0xffff) <= wSize - MIN_LOOKAHEAD ) { // To simplify the code, we prevent matches with the string // of window index 0 (in particular we have to avoid a match // of the string with itself at the start of the input file). if (strategy != Z_HUFFMAN_ONLY) { - match_length = longest_match(hash_head); + matchLength = longestMatch(hashHead); } // longest_match() sets match_start - if (match_length <= 5 && (strategy == Z_FILTERED || - (match_length == MIN_MATCH && - strstart - match_start > 4096))) { + if (matchLength <= 5 && (strategy == Z_FILTERED || + (matchLength == MIN_MATCH && + strStart - matchStart > 4096))) { // If prev_match is also MIN_MATCH, match_start is garbage // but we will ignore the current match anyway. - match_length = MIN_MATCH - 1; + matchLength = MIN_MATCH - 1; } } // If there was a match at the previous step and the current // match is not better, output the previous match: - if (prev_length >= MIN_MATCH && match_length <= prev_length) { - int max_insert = strstart + lookahead - MIN_MATCH; + if (prevLength >= MIN_MATCH && matchLength <= prevLength) { + int maxInsert = strStart + lookahead - MIN_MATCH; // Do not insert strings in hash table beyond this. // check_match(strstart-1, prev_match, prev_length); - bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH); + bflush = trTally(strStart - 1 - prevMatch, prevLength - MIN_MATCH); // Insert in hash table all strings up to the end of the match. // strstart-1 and strstart are already inserted. If there is not // enough lookahead, the last two strings are not inserted in // the hash table. - lookahead -= prev_length - 1; - prev_length -= 2; + lookahead -= prevLength - 1; + prevLength -= 2; do { - if (++strstart <= max_insert) { - ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; - //prev[strstart&w_mask]=hash_head=head[ins_h]; - hash_head = (head[ins_h] & 0xffff); - prev[strstart & w_mask] = head[ins_h]; - head[ins_h] = (short) strstart; + if (++strStart <= maxInsert) { + insH = (((insH) << hashShift) ^ (window[(strStart) + (MIN_MATCH - 1)] & 0xff)) & hashMask; + //prev[strstart&w_mask]=hashHead=head[ins_h]; + hashHead = (head[insH] & 0xffff); + prev[strStart & wMask] = head[insH]; + head[insH] = (short) strStart; } } - while (--prev_length != 0); - match_available = 0; - match_length = MIN_MATCH - 1; - strstart++; + while (--prevLength != 0); + matchAvailable = 0; + matchLength = MIN_MATCH - 1; + strStart++; if (bflush) { - flush_block_only(false); - if (strm.avail_out == 0) return NeedMore; + flushBlockOnly(false); + if (strm.availOut == 0) return NEED_MORE; } - } else if (match_available != 0) { + } else if (matchAvailable != 0) { // If there was no match at the previous position, output a // single literal. If there was a match but the current match // is longer, truncate the previous match to a single literal. - bflush = _tr_tally(0, window[strstart - 1] & 0xff); + bflush = trTally(0, window[strStart - 1] & 0xff); if (bflush) { - flush_block_only(false); + flushBlockOnly(false); } - strstart++; + strStart++; lookahead--; - if (strm.avail_out == 0) return NeedMore; + if (strm.availOut == 0) return NEED_MORE; } else { // There is no previous match to compare with, wait for // the next step to decide. - match_available = 1; - strstart++; + matchAvailable = 1; + strStart++; lookahead--; } } - if (match_available != 0) { - _tr_tally(0, window[strstart - 1] & 0xff); - match_available = 0; + if (matchAvailable != 0) { + trTally(0, window[strStart - 1] & 0xff); + matchAvailable = 0; } - flush_block_only(flush == Z_FINISH); + flushBlockOnly(flush == Z_FINISH); - if (strm.avail_out == 0) { - if (flush == Z_FINISH) return FinishStarted; - else return NeedMore; + if (strm.availOut == 0) { + if (flush == Z_FINISH) return FINISH_STARTED; + else return NEED_MORE; } - return flush == Z_FINISH ? FinishDone : BlockDone; + return flush == Z_FINISH ? FINISH_DONE : BLOCK_DONE; } - int longest_match(int cur_match) { - int chain_length = max_chain_length; // max hash chain length - int scan = strstart; // current string + int longestMatch(int curMatch) { + int chainLength = maxChainLength; // max hash chain length + int scan = strStart; // current string int match; // matched string int len; // length of current match - int best_len = prev_length; // best match length so far - int limit = strstart > (w_size - MIN_LOOKAHEAD) ? - strstart - (w_size - MIN_LOOKAHEAD) : 0; - int nice_match = this.nice_match; + int bestLen = prevLength; // best match length so far + int limit = strStart > (wSize - MIN_LOOKAHEAD) ? + strStart - (wSize - MIN_LOOKAHEAD) : 0; + int niceMatch = this.niceMatch; - // Stop when cur_match becomes <= limit. To simplify the code, + // Stop when curMatch becomes <= limit. To simplify the code, // we prevent matches with the string of window index 0. - int wmask = w_mask; + int wmask = wMask; - int strend = strstart + MAX_MATCH; - byte scan_end1 = window[scan + best_len - 1]; - byte scan_end = window[scan + best_len]; + int strend = strStart + MAX_MATCH; + byte scanEnd1 = window[scan + bestLen - 1]; + byte scanEnd = window[scan + bestLen]; // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. // It is easy to get rid of this optimization if necessary. // Do not waste too much time if we already have a good match: - if (prev_length >= good_match) { - chain_length >>= 2; + if (prevLength >= goodMatch) { + chainLength >>= 2; } // Do not look for matches beyond the end of the input. This is necessary // to make deflate deterministic. - if (nice_match > lookahead) nice_match = lookahead; + if (niceMatch > lookahead) niceMatch = lookahead; do { - match = cur_match; + match = curMatch; // Skip to next match if the match length cannot increase // or if the match length is less than 2: - if (window[match + best_len] != scan_end || - window[match + best_len - 1] != scan_end1 || + if (window[match + bestLen] != scanEnd || + window[match + bestLen - 1] != scanEnd1 || window[match] != window[scan] || window[++match] != window[scan + 1]) continue; - // The check at best_len-1 can be removed because it will be made + // The check at bestLen-1 can be removed because it will be made // again later. (This heuristic is not always a win.) // It is not necessary to compare scan[2] and match[2] since they // are always equal when the other bytes match, given that @@ -1281,18 +1281,18 @@ int longest_match(int cur_match) { len = MAX_MATCH - (strend - scan); scan = strend - MAX_MATCH; - if (len > best_len) { - match_start = cur_match; - best_len = len; - if (len >= nice_match) break; - scan_end1 = window[scan + best_len - 1]; - scan_end = window[scan + best_len]; + if (len > bestLen) { + matchStart = curMatch; + bestLen = len; + if (len >= niceMatch) break; + scanEnd1 = window[scan + bestLen - 1]; + scanEnd = window[scan + bestLen]; } - } while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit - && --chain_length != 0); + } while ((curMatch = (prev[curMatch & wmask] & 0xffff)) > limit + && --chainLength != 0); - if (best_len <= lookahead) return best_len; + if (bestLen <= lookahead) return bestLen; return lookahead; } @@ -1344,28 +1344,28 @@ private int deflateInit(int level, int method, int windowBits, strm.dstate = this; this.wrap = wrap; - w_bits = windowBits; - w_size = 1 << w_bits; - w_mask = w_size - 1; + wBits = windowBits; + wSize = 1 << wBits; + wMask = wSize - 1; - hash_bits = memLevel + 7; - hash_size = 1 << hash_bits; - hash_mask = hash_size - 1; - hash_shift = ((hash_bits + MIN_MATCH - 1) / MIN_MATCH); + hashBits = memLevel + 7; + hashSize = 1 << hashBits; + hashMask = hashSize - 1; + hashShift = ((hashBits + MIN_MATCH - 1) / MIN_MATCH); - window = new byte[w_size * 2]; - prev = new short[w_size]; - head = new short[hash_size]; + window = new byte[wSize * 2]; + prev = new short[wSize]; + head = new short[hashSize]; - lit_bufsize = 1 << (memLevel + 6); // 16K elements by default + litBufSize = 1 << (memLevel + 6); // 16K elements by default // We overlay pending_buf and d_buf+l_buf. This works since the average // output size for (length,distance) codes is <= 24 bits. - pending_buf = new byte[lit_bufsize * 4]; - pending_buf_size = lit_bufsize * 4; + pendingBuf = new byte[litBufSize * 4]; + pendingBufSize = litBufSize * 4; - d_buf = lit_bufsize / 2; - l_buf = (1 + 2) * lit_bufsize; + dBuf = litBufSize / 2; + lBuf = (1 + 2) * litBufSize; this.level = level; @@ -1374,12 +1374,12 @@ private int deflateInit(int level, int method, int windowBits, } int deflateReset() { - strm.total_in = strm.total_out = 0; + strm.totalIn = strm.totalOut = 0; strm.msg = null; // - strm.data_type = Z_UNKNOWN; + strm.dataType = Z_UNKNOWN; pending = 0; - pending_out = 0; + pendingOut = 0; if (wrap < 0) { wrap = -wrap; @@ -1387,10 +1387,10 @@ int deflateReset() { status = (wrap == 0) ? BUSY_STATE : INIT_STATE; strm.adler.reset(); - last_flush = Z_NO_FLUSH; + lastFlush = Z_NO_FLUSH; - tr_init(); - lm_init(); + trInit(); + lmInit(); return Z_OK; } @@ -1399,7 +1399,7 @@ int deflateEnd() { return Z_STREAM_ERROR; } // Deallocate in reverse order of allocations: - pending_buf = null; + pendingBuf = null; head = null; prev = null; window = null; @@ -1408,31 +1408,31 @@ int deflateEnd() { return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; } - int deflateParams(int _level, int _strategy) { + int deflateParams(int levelParam, int strategyParam) { int err = Z_OK; - if (_level == Z_DEFAULT_COMPRESSION) { - _level = 6; + if (levelParam == Z_DEFAULT_COMPRESSION) { + levelParam = 6; } - if (_level < 0 || _level > 9 || - _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) { + if (levelParam < 0 || levelParam > 9 || + strategyParam < 0 || strategyParam > Z_HUFFMAN_ONLY) { return Z_STREAM_ERROR; } - if (config_table[level].func != config_table[_level].func && - strm.total_in != 0) { + if (CONFIG_TABLE[level].func != CONFIG_TABLE[levelParam].func && + strm.totalIn != 0) { // Flush the last buffer: err = strm.deflate(Z_PARTIAL_FLUSH); } - if (level != _level) { - level = _level; - max_lazy_match = config_table[level].max_lazy; - good_match = config_table[level].good_length; - nice_match = config_table[level].nice_length; - max_chain_length = config_table[level].max_chain; + if (level != levelParam) { + level = levelParam; + maxLazyMatch = CONFIG_TABLE[level].maxLazy; + goodMatch = CONFIG_TABLE[level].goodLength; + niceMatch = CONFIG_TABLE[level].niceLength; + maxChainLength = CONFIG_TABLE[level].maxChain; } - strategy = _strategy; + strategy = strategyParam; return err; } @@ -1446,49 +1446,49 @@ int deflateSetDictionary(byte[] dictionary, int dictLength) { strm.adler.update(dictionary, 0, dictLength); if (length < MIN_MATCH) return Z_OK; - if (length > w_size - MIN_LOOKAHEAD) { - length = w_size - MIN_LOOKAHEAD; + if (length > wSize - MIN_LOOKAHEAD) { + length = wSize - MIN_LOOKAHEAD; index = dictLength - length; // use the tail of the dictionary } System.arraycopy(dictionary, index, window, 0, length); - strstart = length; - block_start = length; + strStart = length; + blockStart = length; // Insert all strings in the hash table (except for the last two bytes). // s->lookahead stays null, so s->ins_h will be recomputed at the next // call of fill_window. - ins_h = window[0] & 0xff; - ins_h = (((ins_h) << hash_shift) ^ (window[1] & 0xff)) & hash_mask; + insH = window[0] & 0xff; + insH = (((insH) << hashShift) ^ (window[1] & 0xff)) & hashMask; for (int n = 0; n <= length - MIN_MATCH; n++) { - ins_h = (((ins_h) << hash_shift) ^ (window[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; - prev[n & w_mask] = head[ins_h]; - head[ins_h] = (short) n; + insH = (((insH) << hashShift) ^ (window[(n) + (MIN_MATCH - 1)] & 0xff)) & hashMask; + prev[n & wMask] = head[insH]; + head[insH] = (short) n; } return Z_OK; } int deflate(int flush) { - int old_flush; + int oldFlush; if (flush > Z_FINISH || flush < 0) { return Z_STREAM_ERROR; } - if (strm.next_out == null || - (strm.next_in == null && strm.avail_in != 0) || + if (strm.nextOut == null || + (strm.nextIn == null && strm.availIn != 0) || (status == FINISH_STATE && flush != Z_FINISH)) { - strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)]; + strm.msg = Z_ERRMSG[Z_NEED_DICT - (Z_STREAM_ERROR)]; return Z_STREAM_ERROR; } - if (strm.avail_out == 0) { - strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)]; + if (strm.availOut == 0) { + strm.msg = Z_ERRMSG[Z_NEED_DICT - (Z_BUF_ERROR)]; return Z_BUF_ERROR; } - old_flush = last_flush; - last_flush = flush; + oldFlush = lastFlush; + lastFlush = flush; // Write the zlib header if (status == INIT_STATE) { @@ -1497,12 +1497,12 @@ int deflate(int flush) { status = BUSY_STATE; strm.adler.reset(); } else { - int header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8; - int level_flags = ((level - 1) & 0xff) >> 1; + int header = (Z_DEFLATED + ((wBits - 8) << 4)) << 8; + int levelFlags = ((level - 1) & 0xff) >> 1; - if (level_flags > 3) level_flags = 3; - header |= (level_flags << 6); - if (strstart != 0) header |= PRESET_DICT; + if (levelFlags > 3) levelFlags = 3; + header |= (levelFlags << 6); + if (strStart != 0) header |= PRESET_DICT; header += 31 - (header % 31); status = BUSY_STATE; @@ -1510,7 +1510,7 @@ int deflate(int flush) { // Save the adler32 of the preset dictionary: - if (strstart != 0) { + if (strStart != 0) { long adler = strm.adler.getValue(); putShortMSB((int) (adler >>> 16)); putShortMSB((int) (adler & 0xffff)); @@ -1521,55 +1521,55 @@ int deflate(int flush) { // Flush as much pending output as possible if (pending != 0) { - strm.flush_pending(); - if (strm.avail_out == 0) { + strm.flushPending(); + if (strm.availOut == 0) { // Since avail_out is 0, deflate will be called again with // more output space, but possibly with both pending and // avail_in equal to zero. There won't be anything to do, // but this is not an error situation so make sure we // return OK instead of BUF_ERROR at next call of deflate: - last_flush = -1; + lastFlush = -1; return Z_OK; } // Make sure there is something to do and avoid duplicate consecutive // flushes. For repeated and useless calls with Z_FINISH, we keep // returning Z_STREAM_END instead of Z_BUFF_ERROR. - } else if (strm.avail_in == 0 && flush <= old_flush && + } else if (strm.availIn == 0 && flush <= oldFlush && flush != Z_FINISH) { - strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)]; + strm.msg = Z_ERRMSG[Z_NEED_DICT - (Z_BUF_ERROR)]; return Z_BUF_ERROR; } // User must not provide more input after the first FINISH: - if (status == FINISH_STATE && strm.avail_in != 0) { - strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)]; + if (status == FINISH_STATE && strm.availIn != 0) { + strm.msg = Z_ERRMSG[Z_NEED_DICT - (Z_BUF_ERROR)]; return Z_BUF_ERROR; } // Start a new block or continue the current one. - if (strm.avail_in != 0 || lookahead != 0 || + if (strm.availIn != 0 || lookahead != 0 || (flush != Z_NO_FLUSH && status != FINISH_STATE)) { int bstate = -1; - switch (config_table[level].func) { + switch (CONFIG_TABLE[level].func) { case STORED: - bstate = deflate_stored(flush); + bstate = deflateStored(flush); break; case FAST: - bstate = deflate_fast(flush); + bstate = deflateFast(flush); break; case SLOW: - bstate = deflate_slow(flush); + bstate = deflateSlow(flush); break; default: } - if (bstate == FinishStarted || bstate == FinishDone) { + if (bstate == FINISH_STARTED || bstate == FINISH_DONE) { status = FINISH_STATE; } - if (bstate == NeedMore || bstate == FinishStarted) { - if (strm.avail_out == 0) { - last_flush = -1; // avoid BUF_ERROR next call, see above + if (bstate == NEED_MORE || bstate == FINISH_STARTED) { + if (strm.availOut == 0) { + lastFlush = -1; // avoid BUF_ERROR next call, see above } return Z_OK; // If flush != Z_NO_FLUSH && avail_out == 0, the next call @@ -1580,22 +1580,22 @@ int deflate(int flush) { // one empty block. } - if (bstate == BlockDone) { + if (bstate == BLOCK_DONE) { if (flush == Z_PARTIAL_FLUSH) { - _tr_align(); + trAlign(); } else { // FULL_FLUSH or SYNC_FLUSH - _tr_stored_block(0, 0, false); + trStoredBlock(0, 0, false); // For a full flush, this empty block will be recognized // as a special marker by inflate_sync(). if (flush == Z_FULL_FLUSH) { //state.head[s.hash_size-1]=0; - for (int i = 0; i < hash_size/*-1*/; i++) // forget history + for (int i = 0; i < hashSize/*-1*/; i++) // forget history head[i] = 0; } } - strm.flush_pending(); - if (strm.avail_out == 0) { - last_flush = -1; // avoid BUF_ERROR at next call, see above + strm.flushPending(); + if (strm.availOut == 0) { + lastFlush = -1; // avoid BUF_ERROR at next call, see above return Z_OK; } } @@ -1606,14 +1606,14 @@ int deflate(int flush) { if (wrap == 2) { long adler = strm.adler.getValue(); - put_byte((byte) (adler & 0xff)); - put_byte((byte) ((adler >> 8) & 0xff)); - put_byte((byte) ((adler >> 16) & 0xff)); - put_byte((byte) ((adler >> 24) & 0xff)); - put_byte((byte) (strm.total_in & 0xff)); - put_byte((byte) ((strm.total_in >> 8) & 0xff)); - put_byte((byte) ((strm.total_in >> 16) & 0xff)); - put_byte((byte) ((strm.total_in >> 24) & 0xff)); + putByte((byte) (adler & 0xff)); + putByte((byte) ((adler >> 8) & 0xff)); + putByte((byte) ((adler >> 16) & 0xff)); + putByte((byte) ((adler >> 24) & 0xff)); + putByte((byte) (strm.totalIn & 0xff)); + putByte((byte) ((strm.totalIn >> 8) & 0xff)); + putByte((byte) ((strm.totalIn >> 16) & 0xff)); + putByte((byte) ((strm.totalIn >> 24) & 0xff)); getGZIPHeader().setCRC(adler); } else { @@ -1623,7 +1623,7 @@ int deflate(int flush) { putShortMSB((int) (adler & 0xffff)); } - strm.flush_pending(); + strm.flushPending(); // If avail_out is zero, the application will call deflate again // to flush the rest. @@ -1635,29 +1635,23 @@ int deflate(int flush) { public Object clone() { Deflate dest = new Deflate(strm); - dest.pending_buf = dup(dest.pending_buf); + dest.pendingBuf = dup(dest.pendingBuf); dest.window = dup(dest.window); dest.prev = dup(dest.prev); dest.head = dup(dest.head); - dest.dyn_ltree = dup(dest.dyn_ltree); - dest.dyn_dtree = dup(dest.dyn_dtree); - dest.bl_tree = dup(dest.bl_tree); + dest.dynLtree = dup(dest.dynLtree); + dest.dynDtree = dup(dest.dynDtree); + dest.blTree = dup(dest.blTree); - dest.bl_count = dup(dest.bl_count); - dest.next_code = dup(dest.next_code); + dest.blCount = dup(dest.blCount); + dest.nextCode = dup(dest.nextCode); dest.heap = dup(dest.heap); dest.depth = dup(dest.depth); - dest.l_desc.dyn_tree = dest.dyn_ltree; - dest.d_desc.dyn_tree = dest.dyn_dtree; - dest.bl_desc.dyn_tree = dest.bl_tree; - - /* - dest.l_desc.stat_desc = StaticTree.static_l_desc; - dest.d_desc.stat_desc = StaticTree.static_d_desc; - dest.bl_desc.stat_desc = StaticTree.static_bl_desc; - */ + dest.lDesc.dynTree = dest.dynLtree; + dest.dDesc.dynTree = dest.dynDtree; + dest.blDesc.dynTree = dest.blTree; if (dest.gheader != null) { dest.gheader = (GZIPHeader) dest.gheader.clone(); @@ -1692,18 +1686,18 @@ synchronized GZIPHeader getGZIPHeader() { } static class Config { - int good_length; // reduce lazy search above this match length - int max_lazy; // do not perform lazy search above this match length - int nice_length; // quit search above this match length - int max_chain; + int goodLength; // reduce lazy search above this match length + int maxLazy; // do not perform lazy search above this match length + int niceLength; // quit search above this match length + int maxChain; int func; - Config(int good_length, int max_lazy, - int nice_length, int max_chain, int func) { - this.good_length = good_length; - this.max_lazy = max_lazy; - this.nice_length = nice_length; - this.max_chain = max_chain; + Config(int goodLength, int maxLazy, + int niceLength, int maxChain, int func) { + this.goodLength = goodLength; + this.maxLazy = maxLazy; + this.niceLength = niceLength; + this.maxChain = maxChain; this.func = func; } } diff --git a/CodenameOne/src/com/codename1/io/gzip/DeflaterOutputStream.java b/CodenameOne/src/com/codename1/io/gzip/DeflaterOutputStream.java index eb3a2733a9..7628827108 100644 --- a/CodenameOne/src/com/codename1/io/gzip/DeflaterOutputStream.java +++ b/CodenameOne/src/com/codename1/io/gzip/DeflaterOutputStream.java @@ -84,13 +84,13 @@ public void write(int b) throws IOException { public void write(byte[] b, int off, int len) throws IOException { if (deflater.finished()) { throw new IOException("finished"); - } else if (off < 0 | len < 0 | off + len > b.length) { + } else if (off < 0 || len < 0 || off + len > b.length) { throw new IndexOutOfBoundsException(); } else if (len == 0) { } else { int flush = syncFlush ? JZlib.Z_SYNC_FLUSH : JZlib.Z_NO_FLUSH; deflater.setInput(b, off, len, true); - while (deflater.avail_in > 0) { + while (deflater.availIn > 0) { int err = deflate(flush); if (err == JZlib.Z_STREAM_END) break; @@ -124,14 +124,14 @@ protected int deflate(int flush) throws IOException { case JZlib.Z_STREAM_END: break; case JZlib.Z_BUF_ERROR: - if (deflater.avail_in <= 0 && flush != JZlib.Z_FINISH) { + if (deflater.availIn <= 0 && flush != JZlib.Z_FINISH) { // flush() without any data break; } default: throw new IOException("failed to deflate"); } - int len = deflater.next_out_index; + int len = deflater.nextOutIndex; if (len > 0) { out.write(buffer, 0, len); } @@ -142,7 +142,7 @@ public void flush() throws IOException { if (syncFlush && !deflater.finished()) { while (true) { int err = deflate(JZlib.Z_SYNC_FLUSH); - if (deflater.next_out_index < buffer.length) + if (deflater.nextOutIndex < buffer.length) break; if (err == JZlib.Z_STREAM_END) break; diff --git a/CodenameOne/src/com/codename1/io/gzip/GZIPHeader.java b/CodenameOne/src/com/codename1/io/gzip/GZIPHeader.java index 5df3527623..bc021d2582 100644 --- a/CodenameOne/src/com/codename1/io/gzip/GZIPHeader.java +++ b/CodenameOne/src/com/codename1/io/gzip/GZIPHeader.java @@ -151,30 +151,30 @@ void put(Deflate d) { xfl |= 2; } - d.put_short((short) 0x8b1f); // ID1 ID2 - d.put_byte((byte) 8); // CM(Compression Method) - d.put_byte((byte) flag); - d.put_byte((byte) mtime); - d.put_byte((byte) (mtime >> 8)); - d.put_byte((byte) (mtime >> 16)); - d.put_byte((byte) (mtime >> 24)); - d.put_byte((byte) xfl); - d.put_byte((byte) os); + d.putShort((short) 0x8b1f); // ID1 ID2 + d.putByte((byte) 8); // CM(Compression Method) + d.putByte((byte) flag); + d.putByte((byte) mtime); + d.putByte((byte) (mtime >> 8)); + d.putByte((byte) (mtime >> 16)); + d.putByte((byte) (mtime >> 24)); + d.putByte((byte) xfl); + d.putByte((byte) os); if (extra != null) { - d.put_byte((byte) extra.length); - d.put_byte((byte) (extra.length >> 8)); - d.put_byte(extra, 0, extra.length); + d.putByte((byte) extra.length); + d.putByte((byte) (extra.length >> 8)); + d.putByte(extra, 0, extra.length); } if (name != null) { - d.put_byte(name, 0, name.length); - d.put_byte((byte) 0); + d.putByte(name, 0, name.length); + d.putByte((byte) 0); } if (comment != null) { - d.put_byte(comment, 0, comment.length); - d.put_byte((byte) 0); + d.putByte(comment, 0, comment.length); + d.putByte((byte) 0); } } diff --git a/CodenameOne/src/com/codename1/io/gzip/GZIPInputStream.java b/CodenameOne/src/com/codename1/io/gzip/GZIPInputStream.java index 8c5b6d5fbf..6ea854d8b9 100644 --- a/CodenameOne/src/com/codename1/io/gzip/GZIPInputStream.java +++ b/CodenameOne/src/com/codename1/io/gzip/GZIPInputStream.java @@ -87,8 +87,8 @@ public void readHeader() throws IOException { if (n > 0) { inflater.setInput(b, 0, n, false); //inflater.next_in_index = n; - inflater.next_in_index = 0; - inflater.avail_in = n; + inflater.nextInIndex = 0; + inflater.availIn = n; } throw new IOException("no input"); } @@ -97,7 +97,7 @@ public void readHeader() throws IOException { byte[] b1 = new byte[1]; do { - if (inflater.avail_in <= 0) { + if (inflater.availIn <= 0) { int i = in.read(b1); if (i <= 0) throw new IOException("no input"); @@ -107,19 +107,19 @@ public void readHeader() throws IOException { int err = inflater.inflate(JZlib.Z_NO_FLUSH); if (err != 0/*Z_OK*/) { - int len = 2048 - inflater.next_in.length; + int len = 2048 - inflater.nextIn.length; if (len > 0) { byte[] tmp = new byte[len]; n = fill(tmp); if (n > 0) { - inflater.avail_in += inflater.next_in_index; - inflater.next_in_index = 0; + inflater.availIn += inflater.nextInIndex; + inflater.nextInIndex = 0; inflater.setInput(tmp, 0, n, true); } } //inflater.next_in_index = inflater.next_in.length; - inflater.avail_in += inflater.next_in_index; - inflater.next_in_index = 0; + inflater.availIn += inflater.nextInIndex; + inflater.nextInIndex = 0; throw new IOException(inflater.msg); } } diff --git a/CodenameOne/src/com/codename1/io/gzip/InfBlocks.java b/CodenameOne/src/com/codename1/io/gzip/InfBlocks.java index 7a74329672..66170ce219 100644 --- a/CodenameOne/src/com/codename1/io/gzip/InfBlocks.java +++ b/CodenameOne/src/com/codename1/io/gzip/InfBlocks.java @@ -129,8 +129,8 @@ int proc(int r) { // copy input/output information to locals (UPDATE macro restores) { - p = z.next_in_index; - n = z.avail_in; + p = z.nextInIndex; + n = z.availIn; b = bitb; k = bitk; } @@ -150,14 +150,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } t = b & 7; @@ -209,9 +209,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -224,14 +224,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -242,9 +242,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -256,9 +256,9 @@ int proc(int r) { if (n == 0) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -280,9 +280,9 @@ int proc(int r) { if (m == 0) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -293,7 +293,7 @@ int proc(int r) { t = left; if (t > n) t = n; if (t > m) t = m; - System.arraycopy(z.next_in, p, window, q, t); + System.arraycopy(z.nextIn, p, window, q, t); p += t; n -= t; q += t; @@ -310,14 +310,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -329,9 +329,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -359,14 +359,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -393,9 +393,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -420,14 +420,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -448,14 +448,14 @@ int proc(int r) { } else { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -478,9 +478,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -511,9 +511,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -523,9 +523,9 @@ int proc(int r) { case CODES: bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; if ((r = codes.proc(r)) != Z_STREAM_END) { @@ -534,8 +534,8 @@ int proc(int r) { r = Z_OK; codes.free(z); - p = z.next_in_index; - n = z.avail_in; + p = z.nextInIndex; + n = z.availIn; b = bitb; k = bitk; q = write; @@ -553,9 +553,9 @@ int proc(int r) { if (read != write) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; return inflate_flush(r); } mode = DONE; @@ -564,9 +564,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); case BAD: @@ -574,9 +574,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); @@ -585,9 +585,9 @@ int proc(int r) { bitb = b; bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; write = q; return inflate_flush(r); } @@ -619,17 +619,17 @@ int inflate_flush(int r) { int q; // local copies of source and destination pointers - p = z.next_out_index; + p = z.nextOutIndex; q = read; // compute number of bytes to copy as far as end of window n = (q <= write ? write : end) - q; - if (n > z.avail_out) n = z.avail_out; + if (n > z.availOut) n = z.availOut; if (n != 0 && r == Z_BUF_ERROR) r = Z_OK; // update counters - z.avail_out -= n; - z.total_out += n; + z.availOut -= n; + z.totalOut += n; // update check information if (check && n > 0) { @@ -637,7 +637,7 @@ int inflate_flush(int r) { } // copy as far as end of window - System.arraycopy(window, q, z.next_out, p, n); + System.arraycopy(window, q, z.nextOut, p, n); p += n; q += n; @@ -650,12 +650,12 @@ int inflate_flush(int r) { // compute bytes to copy n = write - q; - if (n > z.avail_out) n = z.avail_out; + if (n > z.availOut) n = z.availOut; if (n != 0 && r == Z_BUF_ERROR) r = Z_OK; // update counters - z.avail_out -= n; - z.total_out += n; + z.availOut -= n; + z.totalOut += n; // update check information if (check && n > 0) { @@ -663,13 +663,13 @@ int inflate_flush(int r) { } // copy - System.arraycopy(window, q, z.next_out, p, n); + System.arraycopy(window, q, z.nextOut, p, n); p += n; q += n; } // update pointers - z.next_out_index = p; + z.nextOutIndex = p; read = q; // done diff --git a/CodenameOne/src/com/codename1/io/gzip/InfCodes.java b/CodenameOne/src/com/codename1/io/gzip/InfCodes.java index 8856aaf5cb..0d58dd66a0 100644 --- a/CodenameOne/src/com/codename1/io/gzip/InfCodes.java +++ b/CodenameOne/src/com/codename1/io/gzip/InfCodes.java @@ -117,8 +117,8 @@ int proc(int r) { int f; // pointer to copy strings from // copy input/output information to locals (UPDATE macro restores) - p = z.next_in_index; - n = z.avail_in; + p = z.nextInIndex; + n = z.availIn; b = s.bitb; k = s.bitk; q = s.write; @@ -133,17 +133,17 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; r = inflate_fast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, s, z); - p = z.next_in_index; - n = z.avail_in; + p = z.nextInIndex; + n = z.availIn; b = s.bitb; k = s.bitk; q = s.write; @@ -168,14 +168,14 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -212,9 +212,9 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); @@ -227,14 +227,14 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -256,14 +256,14 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -290,9 +290,9 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); @@ -305,14 +305,14 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -348,9 +348,9 @@ int proc(int r) { if (m == 0) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } @@ -385,9 +385,9 @@ int proc(int r) { if (m == 0) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } @@ -413,9 +413,9 @@ int proc(int r) { if (s.read != s.write) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } @@ -424,9 +424,9 @@ int proc(int r) { r = Z_STREAM_END; s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); @@ -436,9 +436,9 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); @@ -447,9 +447,9 @@ int proc(int r) { s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return s.inflate_flush(r); } @@ -488,8 +488,8 @@ int inflate_fast(int bl, int bd, int tp_index_t_3; // (tp_index+t)*3 // load input, output, bit values - p = z.next_in_index; - n = z.avail_in; + p = z.nextInIndex; + n = z.availIn; b = s.bitb; k = s.bitk; q = s.write; @@ -504,7 +504,7 @@ int inflate_fast(int bl, int bd, // get literal/length code while (k < (20)) { // max bits for literal/length code n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -535,7 +535,7 @@ int inflate_fast(int bl, int bd, // decode distance base of block to copy while (k < (15)) { // max bits for distance code n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -555,7 +555,7 @@ int inflate_fast(int bl, int bd, e &= 15; while (k < (e)) { // get extra bits (up to 13) n--; - b |= (z.next_in[p++] & 0xff) << k; + b |= (z.nextIn[p++] & 0xff) << k; k += 8; } @@ -624,7 +624,7 @@ int inflate_fast(int bl, int bd, } else { z.msg = "invalid distance code"; - c = z.avail_in - n; + c = z.availIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; @@ -632,9 +632,9 @@ int inflate_fast(int bl, int bd, s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return Z_DATA_ERROR; @@ -659,7 +659,7 @@ int inflate_fast(int bl, int bd, } } else if ((e & 32) != 0) { - c = z.avail_in - n; + c = z.availIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; @@ -667,16 +667,16 @@ int inflate_fast(int bl, int bd, s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return Z_STREAM_END; } else { z.msg = "invalid literal/length code"; - c = z.avail_in - n; + c = z.availIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; @@ -684,9 +684,9 @@ int inflate_fast(int bl, int bd, s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return Z_DATA_ERROR; @@ -697,7 +697,7 @@ int inflate_fast(int bl, int bd, while (m >= 258 && n >= 10); // not enough input or output--restore pointers and return - c = z.avail_in - n; + c = z.availIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; @@ -705,9 +705,9 @@ int inflate_fast(int bl, int bd, s.bitb = b; s.bitk = k; - z.avail_in = n; - z.total_in += p - z.next_in_index; - z.next_in_index = p; + z.availIn = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; s.write = q; return Z_OK; diff --git a/CodenameOne/src/com/codename1/io/gzip/Inflate.java b/CodenameOne/src/com/codename1/io/gzip/Inflate.java index 802ab5cb32..d643af8bb2 100644 --- a/CodenameOne/src/com/codename1/io/gzip/Inflate.java +++ b/CodenameOne/src/com/codename1/io/gzip/Inflate.java @@ -110,7 +110,7 @@ final class Inflate { int inflateReset() { if (z == null) return Z_STREAM_ERROR; - z.total_in = z.total_out = 0; + z.totalIn = z.totalOut = 0; z.msg = null; this.mode = HEAD; this.need_bytes = -1; @@ -171,7 +171,7 @@ int inflate(int f) { int r; int b; - if (z == null || z.next_in == null) { + if (z == null || z.nextIn == null) { if (f == Z_FINISH && this.mode == HEAD) return Z_OK; return Z_STREAM_ERROR; @@ -224,9 +224,9 @@ int inflate(int f) { (((this.method << 8) + b) % 31) != 0) && (this.method & 0xf) != Z_DEFLATED) { if (wrap == 4) { - z.next_in_index -= 2; - z.avail_in += 2; - z.total_in -= 2; + z.nextInIndex -= 2; + z.availIn += 2; + z.totalIn -= 2; wrap = 0; this.mode = BLOCKS; break; @@ -273,36 +273,36 @@ int inflate(int f) { this.mode = DICT4; case DICT4: - if (z.avail_in == 0) return r; - z.avail_in--; - z.total_in++; - this.need = ((long) (z.next_in[z.next_in_index++] & 0xff) << 24) & 0xff000000L; + if (z.availIn == 0) return r; + z.availIn--; + z.totalIn++; + this.need = ((long) (z.nextIn[z.nextInIndex++] & 0xff) << 24) & 0xff000000L; this.mode = DICT3; case DICT3: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need += ((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L; + z.availIn--; + z.totalIn++; + this.need += ((z.nextIn[z.nextInIndex++] & 0xff) << 16) & 0xff0000L; this.mode = DICT2; case DICT2: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need += ((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L; + z.availIn--; + z.totalIn++; + this.need += ((z.nextIn[z.nextInIndex++] & 0xff) << 8) & 0xff00L; this.mode = DICT1; case DICT1: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; - z.avail_in--; - z.total_in++; - this.need += (z.next_in[z.next_in_index++] & 0xffL); + z.availIn--; + z.totalIn++; + this.need += (z.nextIn[z.nextInIndex++] & 0xffL); z.adler.reset(this.need); this.mode = DICT0; return Z_NEED_DICT; @@ -336,39 +336,39 @@ int inflate(int f) { this.mode = CHECK4; case CHECK4: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need = ((long) (z.next_in[z.next_in_index++] & 0xff) << 24) & 0xff000000L; + z.availIn--; + z.totalIn++; + this.need = ((long) (z.nextIn[z.nextInIndex++] & 0xff) << 24) & 0xff000000L; this.mode = CHECK3; case CHECK3: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need += ((z.next_in[z.next_in_index++] & 0xff) << 16) & 0xff0000L; + z.availIn--; + z.totalIn++; + this.need += ((z.nextIn[z.nextInIndex++] & 0xff) << 16) & 0xff0000L; this.mode = CHECK2; case CHECK2: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need += ((z.next_in[z.next_in_index++] & 0xff) << 8) & 0xff00L; + z.availIn--; + z.totalIn++; + this.need += ((z.nextIn[z.nextInIndex++] & 0xff) << 8) & 0xff00L; this.mode = CHECK1; case CHECK1: - if (z.avail_in == 0) return r; + if (z.availIn == 0) return r; r = f; - z.avail_in--; - z.total_in++; - this.need += (z.next_in[z.next_in_index++] & 0xffL); + z.availIn--; + z.totalIn++; + this.need += (z.nextIn[z.nextInIndex++] & 0xffL); if (flags != 0) { // gzip this.need = ((this.need & 0xff000000) >> 24 | @@ -405,7 +405,7 @@ int inflate(int f) { break; } - if (this.need != (z.total_out & 0xffffffffL)) { + if (this.need != (z.totalOut & 0xffffffffL)) { z.msg = "incorrect length check"; this.mode = BAD; break; @@ -615,15 +615,15 @@ int inflateSync() { this.mode = BAD; this.marker = 0; } - if ((n = z.avail_in) == 0) + if ((n = z.availIn) == 0) return Z_BUF_ERROR; - p = z.next_in_index; + p = z.nextInIndex; // search while (n != 0 && this.marker < 4) { - if (z.next_in[p] == mark[this.marker]) { + if (z.nextIn[p] == mark[this.marker]) { this.marker++; - } else if (z.next_in[p] != 0) { + } else if (z.nextIn[p] != 0) { this.marker = 0; } else { this.marker = 4 - this.marker; @@ -633,19 +633,19 @@ int inflateSync() { } // restore - z.total_in += p - z.next_in_index; - z.next_in_index = p; - z.avail_in = n; + z.totalIn += p - z.nextInIndex; + z.nextInIndex = p; + z.availIn = n; // return no joy or set up to restart on a new block if (this.marker != 4) { return Z_DATA_ERROR; } - r = z.total_in; - w = z.total_out; + r = z.totalIn; + w = z.totalOut; inflateReset(); - z.total_in = r; - z.total_out = w; + z.totalIn = r; + z.totalOut = w; this.mode = BLOCKS; return Z_OK; @@ -669,14 +669,14 @@ private int readBytes(int n, int r, int f) throws Return { this.need = 0; } while (need_bytes > 0) { - if (z.avail_in == 0) { + if (z.availIn == 0) { throw new Return(r); } r = f; - z.avail_in--; - z.total_in++; + z.availIn--; + z.totalIn++; this.need = this.need | - ((long) (z.next_in[z.next_in_index++] & 0xff) << ((n - need_bytes) * 8)); + ((long) (z.nextIn[z.nextInIndex++] & 0xff) << ((n - need_bytes) * 8)); need_bytes--; } if (n == 2) { @@ -694,16 +694,16 @@ private int readString(int r, int f) throws Return { } int b = 0; do { - if (z.avail_in == 0) { + if (z.availIn == 0) { throw new Return(r); } r = f; - z.avail_in--; - z.total_in++; - b = z.next_in[z.next_in_index]; - if (b != 0) tmp_string.write(z.next_in, z.next_in_index, 1); - z.adler.update(z.next_in, z.next_in_index, 1); - z.next_in_index++; + z.availIn--; + z.totalIn++; + b = z.nextIn[z.nextInIndex]; + if (b != 0) tmp_string.write(z.nextIn, z.nextInIndex, 1); + z.adler.update(z.nextIn, z.nextInIndex, 1); + z.nextInIndex++; } while (b != 0); return r; } @@ -713,15 +713,15 @@ private int readBytes(int r, int f) throws Return { tmp_string = new java.io.ByteArrayOutputStream(); } while (this.need > 0) { - if (z.avail_in == 0) { + if (z.availIn == 0) { throw new Return(r); } r = f; - z.avail_in--; - z.total_in++; - tmp_string.write(z.next_in, z.next_in_index, 1); - z.adler.update(z.next_in, z.next_in_index, 1); - z.next_in_index++; + z.availIn--; + z.totalIn++; + tmp_string.write(z.nextIn, z.nextInIndex, 1); + z.adler.update(z.nextIn, z.nextInIndex, 1); + z.nextInIndex++; this.need--; } return r; diff --git a/CodenameOne/src/com/codename1/io/gzip/InflaterInputStream.java b/CodenameOne/src/com/codename1/io/gzip/InflaterInputStream.java index abd5e2616b..4cc910730c 100644 --- a/CodenameOne/src/com/codename1/io/gzip/InflaterInputStream.java +++ b/CodenameOne/src/com/codename1/io/gzip/InflaterInputStream.java @@ -100,11 +100,11 @@ public int read(byte[] b, int off, int len) throws IOException { int n = 0; inflater.setOutput(b, off, len); while (!eof) { - if (inflater.avail_in == 0) + if (inflater.availIn == 0) fill(); int err = inflater.inflate(JZlib.Z_NO_FLUSH); - n += inflater.next_out_index - off; - off = inflater.next_out_index; + n += inflater.nextOutIndex - off; + off = inflater.nextOutIndex; switch (err) { case JZlib.Z_DATA_ERROR: throw new IOException(inflater.msg); @@ -116,7 +116,7 @@ public int read(byte[] b, int off, int len) throws IOException { break; default: } - if (inflater.avail_out == 0) + if (inflater.availOut == 0) break; } return n; @@ -208,11 +208,11 @@ public long getTotalOut() { } public byte[] getAvailIn() { - if (inflater.avail_in <= 0) + if (inflater.availIn <= 0) return null; - byte[] tmp = new byte[inflater.avail_in]; - System.arraycopy(inflater.next_in, inflater.next_in_index, - tmp, 0, inflater.avail_in); + byte[] tmp = new byte[inflater.availIn]; + System.arraycopy(inflater.nextIn, inflater.nextInIndex, + tmp, 0, inflater.availIn); return tmp; } diff --git a/CodenameOne/src/com/codename1/io/gzip/StaticTree.java b/CodenameOne/src/com/codename1/io/gzip/StaticTree.java index f2284ffd19..f65d6d8465 100644 --- a/CodenameOne/src/com/codename1/io/gzip/StaticTree.java +++ b/CodenameOne/src/com/codename1/io/gzip/StaticTree.java @@ -37,7 +37,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING final class StaticTree { // Bit length codes must not exceed MAX_BL_BITS bits static final int MAX_BL_BITS = 7; - static final short[] static_ltree = { + static final short[] STATIC_LTREE = { 12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8, 28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, @@ -97,7 +97,7 @@ final class StaticTree { 3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8 }; - static final short[] static_dtree = { + static final short[] STATIC_DTREE = { 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5, 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, @@ -111,33 +111,33 @@ final class StaticTree { static final private int LITERALS = 256; static final private int LENGTH_CODES = 29; static final private int L_CODES = (LITERALS + 1 + LENGTH_CODES); - static StaticTree static_l_desc = - new StaticTree(static_ltree, Tree.extra_lbits, + static StaticTree staticLDesc = + new StaticTree(STATIC_LTREE, Tree.EXTRA_LBITS, LITERALS + 1, L_CODES, MAX_BITS); - static StaticTree static_d_desc = - new StaticTree(static_dtree, Tree.extra_dbits, + static StaticTree staticDDesc = + new StaticTree(STATIC_DTREE, Tree.EXTRA_DBITS, 0, D_CODES, MAX_BITS); - static StaticTree static_bl_desc = - new StaticTree(null, Tree.extra_blbits, + static StaticTree staticBlDesc = + new StaticTree(null, Tree.EXTRA_BLBITS, 0, BL_CODES, MAX_BL_BITS); - short[] static_tree; // static tree or null - int[] extra_bits; // extra bits for each code or null - int extra_base; // base index for extra_bits + short[] staticTree; // static tree or null + int[] extraBits; // extra bits for each code or null + int extraBase; // base index for extra_bits int elems; // max number of elements in the tree - int max_length; // max bit length for the codes + int maxLength; // max bit length for the codes - private StaticTree(short[] static_tree, - int[] extra_bits, - int extra_base, + private StaticTree(short[] staticTree, + int[] extraBits, + int extraBase, int elems, - int max_length) { - this.static_tree = static_tree; - this.extra_bits = extra_bits; - this.extra_base = extra_base; + int maxLength) { + this.staticTree = staticTree; + this.extraBits = extraBits; + this.extraBase = extraBase; this.elems = elems; - this.max_length = max_length; + this.maxLength = maxLength; } } diff --git a/CodenameOne/src/com/codename1/io/gzip/Tree.java b/CodenameOne/src/com/codename1/io/gzip/Tree.java index 6adbdf3fee..562eeb0bf8 100644 --- a/CodenameOne/src/com/codename1/io/gzip/Tree.java +++ b/CodenameOne/src/com/codename1/io/gzip/Tree.java @@ -46,23 +46,23 @@ final class Tree { // repeat a zero length 11-138 times (7 bits of repeat count) static final int REPZ_11_138 = 18; // extra bits for each length code - static final int[] extra_lbits = { + static final int[] EXTRA_LBITS = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; // extra bits for each distance code - static final int[] extra_dbits = { + static final int[] EXTRA_DBITS = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }; // extra bits for each bit length code - static final int[] extra_blbits = { + static final int[] EXTRA_BLBITS = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; - static final byte[] bl_order = { + static final byte[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; static final int Buf_size = 8 * 2; // see definition of array dist_code below static final int DIST_CODE_LEN = 512; - static final byte[] _dist_code = { + static final byte[] DIST_CODE = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, @@ -90,7 +90,7 @@ final class Tree { 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 }; - static final byte[] _length_code = { + static final byte[] LENGTH_CODE = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, @@ -105,11 +105,11 @@ final class Tree { 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 }; - static final int[] base_length = { + static final int[] BASE_LENGTH = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0 }; - static final int[] base_dist = { + static final int[] BASE_DIST = { 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 @@ -126,57 +126,57 @@ final class Tree { static final private int LENGTH_CODES = 29; static final private int L_CODES = (LITERALS + 1 + LENGTH_CODES); static final private int HEAP_SIZE = (2 * L_CODES + 1); - short[] dyn_tree; // the dynamic tree - int max_code; // largest code with non zero frequency - StaticTree stat_desc; // the corresponding static tree + short[] dynTree; // the dynamic tree + int maxCode; // largest code with non zero frequency + StaticTree statDesc; // the corresponding static tree // Mapping from a distance to a distance code. dist is the distance - 1 and // must not have side effects. _dist_code[256] and _dist_code[257] are never // used. - static int d_code(int dist) { - return ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >>> 7)]); + static int dCode(int dist) { + return ((dist) < 256 ? DIST_CODE[dist] : DIST_CODE[256 + ((dist) >>> 7)]); } // Generate the codes for a given tree and bit counts (which need not be // optimal). - // IN assertion: the array bl_count contains the bit length statistics for + // IN assertion: the array blCount contains the bit length statistics for // the given tree and the field len is set for all tree elements. // OUT assertion: the field code is set for all tree elements of non // zero code length. - private static void gen_codes( + private static void genCodes( short[] tree, // the tree to decorate - int max_code, // largest code with non zero frequency - short[] bl_count, // number of codes at each bit length - short[] next_code) { + int maxCode, // largest code with non zero frequency + short[] blCount, // number of codes at each bit length + short[] nextCode) { short code = 0; // running code value int bits; // bit index int n; // code index // The distribution counts are first used to generate the code values // without bit reversal. - next_code[0] = 0; + nextCode[0] = 0; for (bits = 1; bits <= MAX_BITS; bits++) { - next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1); + nextCode[bits] = code = (short) ((code + blCount[bits - 1]) << 1); } - // Check that the bit counts in bl_count are consistent. The last code + // Check that the bit counts in blCount are consistent. The last code // must be all ones. - //Assert (code + bl_count[MAX_BITS]-1 == (1< max_length) { - bits = max_length; + if (bits > maxLength) { + bits = maxLength; overflow++; } tree[n * 2 + 1] = (short) bits; // We overwrite tree[n*2+1] which is no longer needed - if (n > max_code) continue; // not a leaf node + if (n > maxCode) continue; // not a leaf node - s.bl_count[bits]++; + s.blCount[bits]++; xbits = 0; if (n >= base) xbits = extra[n - base]; f = tree[n * 2]; - s.opt_len += f * (bits + xbits); - if (stree != null) s.static_len += f * (stree[n * 2 + 1] + xbits); + s.optLen += f * (bits + xbits); + if (stree != null) s.staticLen += f * (stree[n * 2 + 1] + xbits); } if (overflow == 0) return; // This happens for example on obj2 and pic of the Calgary corpus // Find the first bit length which could increase: do { - bits = max_length - 1; - while (s.bl_count[bits] == 0) bits--; - s.bl_count[bits]--; // move one leaf down the tree - s.bl_count[bits + 1] += 2; // move one overflow item as its brother - s.bl_count[max_length]--; + bits = maxLength - 1; + while (s.blCount[bits] == 0) bits--; + s.blCount[bits]--; // move one leaf down the tree + s.blCount[bits + 1] += 2; // move one overflow item as its brother + s.blCount[maxLength]--; // The brother of the overflow item also moves one step up, - // but this does not affect bl_count[max_length] + // but this does not affect bl_count[maxLength] overflow -= 2; } while (overflow > 0); - for (bits = max_length; bits != 0; bits--) { - n = s.bl_count[bits]; + for (bits = maxLength; bits != 0; bits--) { + n = s.blCount[bits]; while (n != 0) { m = s.heap[--h]; - if (m > max_code) continue; + if (m > maxCode) continue; if (tree[m * 2 + 1] != bits) { - s.opt_len += ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]; + s.optLen += ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]; tree[m * 2 + 1] = (short) bits; } n--; @@ -273,23 +273,23 @@ void gen_bitlen(Deflate s) { // OUT assertions: the fields len and code are set to the optimal bit length // and corresponding code. The length opt_len is updated; static_len is // also updated if stree is not null. The field max_code is set. - void build_tree(Deflate s) { - short[] tree = dyn_tree; - short[] stree = stat_desc != null ? stat_desc.static_tree : null; - int elems = stat_desc != null ? stat_desc.elems : 0; + void buildTree(Deflate s) { + short[] tree = dynTree; + short[] stree = statDesc != null ? statDesc.staticTree : null; + int elems = statDesc != null ? statDesc.elems : 0; int n, m; // iterate over heap elements - int max_code = -1; // largest code with non zero frequency + int maxCode = -1; // largest code with non zero frequency int node; // new node being created // Construct the initial heap, with least frequent element in // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. // heap[0] is not used. - s.heap_len = 0; - s.heap_max = HEAP_SIZE; + s.heapLen = 0; + s.heapMax = HEAP_SIZE; for (n = 0; n < elems; n++) { if (tree[n * 2] != 0) { - s.heap[++s.heap_len] = max_code = n; + s.heap[++s.heapLen] = maxCode = n; s.depth[n] = 0; } else { tree[n * 2 + 1] = 0; @@ -300,20 +300,20 @@ void build_tree(Deflate s) { // and that at least one bit should be sent even if there is only one // possible code. So to avoid special checks later on we force at least // two codes of non zero frequency. - while (s.heap_len < 2) { - node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); + while (s.heapLen < 2) { + node = s.heap[++s.heapLen] = (maxCode < 2 ? ++maxCode : 0); tree[node * 2] = 1; s.depth[node] = 0; - s.opt_len--; - if (stree != null) s.static_len -= stree[node * 2 + 1]; + s.optLen--; + if (stree != null) s.staticLen -= stree[node * 2 + 1]; // node is 0 or 1 so it does not have extra bits } - this.max_code = max_code; + this.maxCode = maxCode; // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, // establish sub-heaps of increasing lengths: - for (n = s.heap_len / 2; n >= 1; n--) + for (n = s.heapLen / 2; n >= 1; n--) s.pqdownheap(tree, n); // Construct the Huffman tree by repeatedly combining the least two @@ -323,12 +323,12 @@ void build_tree(Deflate s) { do { // n = node of least frequency n = s.heap[1]; - s.heap[1] = s.heap[s.heap_len--]; + s.heap[1] = s.heap[s.heapLen--]; s.pqdownheap(tree, 1); m = s.heap[1]; // m = node of next least frequency - s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency - s.heap[--s.heap_max] = m; + s.heap[--s.heapMax] = n; // keep the nodes sorted by frequency + s.heap[--s.heapMax] = m; // Create a new node father of n and m tree[node * 2] = (short) (tree[n * 2] + tree[m * 2]); @@ -339,17 +339,17 @@ void build_tree(Deflate s) { s.heap[1] = node++; s.pqdownheap(tree, 1); } - while (s.heap_len >= 2); + while (s.heapLen >= 2); - s.heap[--s.heap_max] = s.heap[1]; + s.heap[--s.heapMax] = s.heap[1]; // At this point, the fields freq and dad are set. We can now // generate the bit lengths. - gen_bitlen(s); + genBitLen(s); // The field len is now set, we can generate the bit codes - gen_codes(tree, max_code, s.bl_count, s.next_code); + genCodes(tree, maxCode, s.blCount, s.nextCode); } } diff --git a/CodenameOne/src/com/codename1/io/gzip/ZStream.java b/CodenameOne/src/com/codename1/io/gzip/ZStream.java index 6ae8996c0f..9be2f77c53 100644 --- a/CodenameOne/src/com/codename1/io/gzip/ZStream.java +++ b/CodenameOne/src/com/codename1/io/gzip/ZStream.java @@ -64,22 +64,22 @@ public class ZStream { static final private int Z_BUF_ERROR = -5; static final private int Z_VERSION_ERROR = -6; - public byte[] next_in; // next input byte - public int next_in_index; - public int avail_in; // number of bytes available at next_in - public long total_in; // total nb of input bytes read so far + public byte[] nextIn; // next input byte + public int nextInIndex; + public int availIn; // number of bytes available at next_in + public long totalIn; // total nb of input bytes read so far - public byte[] next_out; // next output byte should be put there - public int next_out_index; - public int avail_out; // remaining free space at next_out - public long total_out; // total nb of bytes output so far + public byte[] nextOut; // next output byte should be put there + public int nextOutIndex; + public int availOut; // remaining free space at next_out + public long totalOut; // total nb of bytes output so far public String msg; Deflate dstate; Inflate istate; - int data_type; // best guess about the data type: ascii or binary + int dataType; // best guess about the data type: ascii or binary Checksum adler; @@ -223,22 +223,22 @@ public int deflateSetDictionary(byte[] dictionary, int dictLength) { // through this function so some applications may wish to modify it // to avoid allocating a large strm->next_out buffer and copying into it. // (See also read_buf()). - void flush_pending() { + void flushPending() { int len = dstate.pending; - if (len > avail_out) len = avail_out; + if (len > availOut) len = availOut; if (len == 0) return; - System.arraycopy(dstate.pending_buf, dstate.pending_out, - next_out, next_out_index, len); + System.arraycopy(dstate.pendingBuf, dstate.pendingOut, + nextOut, nextOutIndex, len); - next_out_index += len; - dstate.pending_out += len; - total_out += len; - avail_out -= len; + nextOutIndex += len; + dstate.pendingOut += len; + totalOut += len; + availOut -= len; dstate.pending -= len; if (dstate.pending == 0) { - dstate.pending_out = 0; + dstate.pendingOut = 0; } } @@ -248,19 +248,19 @@ void flush_pending() { // allocating a large strm->next_in buffer and copying from it. // (See also flush_pending()). int read_buf(byte[] buf, int start, int size) { - int len = avail_in; + int len = availIn; if (len > size) len = size; if (len == 0) return 0; - avail_in -= len; + availIn -= len; if (dstate.wrap != 0) { - adler.update(next_in, next_in_index, len); + adler.update(nextIn, nextInIndex, len); } - System.arraycopy(next_in, next_in_index, buf, start, len); - next_in_index += len; - total_in += len; + System.arraycopy(nextIn, nextInIndex, buf, start, len); + nextInIndex += len; + totalIn += len; return len; } @@ -269,8 +269,8 @@ public long getAdler() { } public void free() { - next_in = null; - next_out = null; + nextIn = null; + nextOut = null; msg = null; } @@ -279,9 +279,9 @@ public void setOutput(byte[] buf) { } public void setOutput(byte[] buf, int off, int len) { - next_out = buf; - next_out_index = off; - avail_out = len; + nextOut = buf; + nextOutIndex = off; + availOut = len; } public void setInput(byte[] buf) { @@ -293,77 +293,77 @@ public void setInput(byte[] buf, boolean append) { } public void setInput(byte[] buf, int off, int len, boolean append) { - if (len <= 0 && append && next_in != null) return; - - if (avail_in > 0 && append) { - byte[] tmp = new byte[avail_in + len]; - System.arraycopy(next_in, next_in_index, tmp, 0, avail_in); - System.arraycopy(buf, off, tmp, avail_in, len); - next_in = tmp; - next_in_index = 0; - avail_in += len; + if (len <= 0 && append && nextIn != null) return; + + if (availIn > 0 && append) { + byte[] tmp = new byte[availIn + len]; + System.arraycopy(nextIn, nextInIndex, tmp, 0, availIn); + System.arraycopy(buf, off, tmp, availIn, len); + nextIn = tmp; + nextInIndex = 0; + availIn += len; } else { - next_in = buf; - next_in_index = off; - avail_in = len; + nextIn = buf; + nextInIndex = off; + availIn = len; } } public byte[] getNextIn() { - return next_in; + return nextIn; } public void setNextIn(byte[] next_in) { - this.next_in = next_in; + this.nextIn = next_in; } public int getNextInIndex() { - return next_in_index; + return nextInIndex; } public void setNextInIndex(int next_in_index) { - this.next_in_index = next_in_index; + this.nextInIndex = next_in_index; } public int getAvailIn() { - return avail_in; + return availIn; } public void setAvailIn(int avail_in) { - this.avail_in = avail_in; + this.availIn = avail_in; } public byte[] getNextOut() { - return next_out; + return nextOut; } public void setNextOut(byte[] next_out) { - this.next_out = next_out; + this.nextOut = next_out; } public int getNextOutIndex() { - return next_out_index; + return nextOutIndex; } public void setNextOutIndex(int next_out_index) { - this.next_out_index = next_out_index; + this.nextOutIndex = next_out_index; } public int getAvailOut() { - return avail_out; + return availOut; } public void setAvailOut(int avail_out) { - this.avail_out = avail_out; + this.availOut = avail_out; } public long getTotalOut() { - return total_out; + return totalOut; } public long getTotalIn() { - return total_in; + return totalIn; } public String getMessage() { diff --git a/CodenameOne/src/com/codename1/io/rest/RequestBuilder.java b/CodenameOne/src/com/codename1/io/rest/RequestBuilder.java index a143338ed6..95ac69491c 100644 --- a/CodenameOne/src/com/codename1/io/rest/RequestBuilder.java +++ b/CodenameOne/src/com/codename1/io/rest/RequestBuilder.java @@ -23,6 +23,7 @@ */ package com.codename1.io.rest; +import com.codename1.compat.java.util.Objects; import com.codename1.io.ConnectionRequest; import com.codename1.io.Data; import com.codename1.io.JSONParser; @@ -249,8 +250,8 @@ public RequestBuilder queryParam(String key, String[] values) { */ public RequestBuilder header(String key, String value) { checkFetched(); - // .toString() is used to trigger an NPE early for null headers - headers.put(key.toString(), value.toString()); + headers.put(Objects.requireNonNull(key, "Header key cannot be null"), + Objects.requireNonNull(value, "Header value cannot be null")); return this; } diff --git a/CodenameOne/src/com/codename1/io/services/CachedDataService.java b/CodenameOne/src/com/codename1/io/services/CachedDataService.java index 8dae13975b..721b58d312 100644 --- a/CodenameOne/src/com/codename1/io/services/CachedDataService.java +++ b/CodenameOne/src/com/codename1/io/services/CachedDataService.java @@ -38,7 +38,7 @@ * * @author Shai Almog */ -public class CachedDataService extends ConnectionRequest { +public final class CachedDataService extends ConnectionRequest { private final CachedData data = new CachedData(); private boolean responseProcessed; diff --git a/CodenameOne/src/com/codename1/location/GeofenceManager.java b/CodenameOne/src/com/codename1/location/GeofenceManager.java index 5d8dd9018d..347c12a8af 100644 --- a/CodenameOne/src/com/codename1/location/GeofenceManager.java +++ b/CodenameOne/src/com/codename1/location/GeofenceManager.java @@ -82,7 +82,7 @@ * * @author shannah */ -public class GeofenceManager implements Iterable { +public final class GeofenceManager implements Iterable { //private GeoStreamerAsyncDataSource dataSource; private static final String STORAGE_KEY = "$AsyncGeoStreamer.geofences$"; private static final String ACTIVE_FENCES_KEY = "$AsyncGeoStreamer.activegeofences$"; diff --git a/CodenameOne/src/com/codename1/location/LocationManager.java b/CodenameOne/src/com/codename1/location/LocationManager.java index cb29be9d96..1cd246771c 100644 --- a/CodenameOne/src/com/codename1/location/LocationManager.java +++ b/CodenameOne/src/com/codename1/location/LocationManager.java @@ -52,6 +52,7 @@ public abstract class LocationManager { public static final int OUT_OF_SERVICE = 1; public static final int TEMPORARILY_UNAVAILABLE = 2; private static LocationListener listener; + private static final Object LISTENER_LOCK = new Object(); private LocationRequest request; private static Class backgroundlistener; private int status = TEMPORARILY_UNAVAILABLE; @@ -198,7 +199,7 @@ protected LocationListener getLocationListener() { * from getting updates */ public void setLocationListener(final LocationListener l) { - synchronized (this) { + synchronized (LISTENER_LOCK) { if (listener != null) { clearListener(); request = null; @@ -243,7 +244,7 @@ protected Class getBackgroundLocationListener() { * try to create an instance and invoke the locationUpdated method */ public void setBackgroundLocationListener(Class locationListener) { - synchronized (this) { + synchronized (LISTENER_LOCK) { if (backgroundlistener != null) { clearBackgroundListener(); } diff --git a/CodenameOne/src/com/codename1/processing/PrettyPrinter.java b/CodenameOne/src/com/codename1/processing/PrettyPrinter.java index 742abd064d..b77d722ea2 100644 --- a/CodenameOne/src/com/codename1/processing/PrettyPrinter.java +++ b/CodenameOne/src/com/codename1/processing/PrettyPrinter.java @@ -52,7 +52,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * * @author Eric Coolman (2012-03 - derivative work from original Sun source). */ -class PrettyPrinter { +final class PrettyPrinter { Map myHashMap; private PrettyPrinter(Map h) { diff --git a/CodenameOne/src/com/codename1/processing/Result.java b/CodenameOne/src/com/codename1/processing/Result.java index 96eddd8be2..2bb43575f9 100644 --- a/CodenameOne/src/com/codename1/processing/Result.java +++ b/CodenameOne/src/com/codename1/processing/Result.java @@ -92,7 +92,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * * @author Eric Coolman (2012-03 - derivative work from original Sun source). */ -public class Result { +public final class Result { public static final String JSON = "json"; public static final String XML = "xml"; diff --git a/CodenameOne/src/com/codename1/properties/InstantUI.java b/CodenameOne/src/com/codename1/properties/InstantUI.java index 6802aca598..b13fc8e6c1 100644 --- a/CodenameOne/src/com/codename1/properties/InstantUI.java +++ b/CodenameOne/src/com/codename1/properties/InstantUI.java @@ -81,7 +81,7 @@ public void excludeProperties(PropertyBase... exclude) { * @return true if the property was excluded from the GUI */ public boolean isExcludedProperty(PropertyBase exclude) { - return exclude.getClientProperty("cn1$excludeFromUI") == Boolean.TRUE; + return Boolean.TRUE.equals(exclude.getClientProperty("cn1$excludeFromUI")); } /** diff --git a/CodenameOne/src/com/codename1/properties/PreferencesObject.java b/CodenameOne/src/com/codename1/properties/PreferencesObject.java index 5109441045..d3230c54ee 100644 --- a/CodenameOne/src/com/codename1/properties/PreferencesObject.java +++ b/CodenameOne/src/com/codename1/properties/PreferencesObject.java @@ -32,7 +32,7 @@ * * @author Shai Almog */ -public class PreferencesObject { +public final class PreferencesObject { private PropertyBusinessObject bo; private String prefix; private boolean bound; diff --git a/CodenameOne/src/com/codename1/properties/PropertyIndex.java b/CodenameOne/src/com/codename1/properties/PropertyIndex.java index 9ed9fe4770..1956334faf 100644 --- a/CodenameOne/src/com/codename1/properties/PropertyIndex.java +++ b/CodenameOne/src/com/codename1/properties/PropertyIndex.java @@ -55,6 +55,7 @@ import java.util.List; import java.util.Map; import java.util.Enumeration; +import java.util.NoSuchElementException; /** * Maps the properties that are in a class/object and provides access to them so tools such as ORM @@ -198,9 +199,10 @@ public void remove() { } public PropertyBase next() { - int i = off; - off++; - return properties[i]; + if (!hasNext()) { + throw new NoSuchElementException(); + } + return properties[off++]; } }; } diff --git a/CodenameOne/src/com/codename1/properties/PropertyXMLElement.java b/CodenameOne/src/com/codename1/properties/PropertyXMLElement.java index 54601eed8f..814dd8600c 100644 --- a/CodenameOne/src/com/codename1/properties/PropertyXMLElement.java +++ b/CodenameOne/src/com/codename1/properties/PropertyXMLElement.java @@ -23,12 +23,14 @@ package com.codename1.properties; +import com.codename1.compat.java.util.Objects; import com.codename1.io.Util; import com.codename1.xml.Element; import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Vector; /** @@ -295,7 +297,21 @@ public boolean isEmpty() { @Override public int hashCode() { - return parent.hashCode(); + return Objects.hash(parent, parentElement, index); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof PropertyXMLElement)) { + return false; + } + PropertyXMLElement other = (PropertyXMLElement) obj; + return index == other.index + && Objects.equals(parent, other.parent) + && Objects.equals(parentElement, other.parentElement); } @Override @@ -330,8 +346,10 @@ public boolean hasNext() { @Override public Element next() { - offset++; - return getChildAt(index); + if (!hasNext()) { + throw new NoSuchElementException(); + } + return getChildAt(offset++); } @Override diff --git a/CodenameOne/src/com/codename1/properties/SQLMap.java b/CodenameOne/src/com/codename1/properties/SQLMap.java index 0ef30936c7..5cf3294965 100644 --- a/CodenameOne/src/com/codename1/properties/SQLMap.java +++ b/CodenameOne/src/com/codename1/properties/SQLMap.java @@ -42,7 +42,7 @@ * * @author Shai Almog */ -public class SQLMap { +public final class SQLMap { private boolean verbose = true; private Database db; diff --git a/CodenameOne/src/com/codename1/push/PushContent.java b/CodenameOne/src/com/codename1/push/PushContent.java index dd2d08b1d7..03f0ae7082 100644 --- a/CodenameOne/src/com/codename1/push/PushContent.java +++ b/CodenameOne/src/com/codename1/push/PushContent.java @@ -30,7 +30,7 @@ * * @author Steve Hannah */ -public class PushContent { +public final class PushContent { private static final String PROP_PREFIX = "com.codename1.push.prop."; private final String title; diff --git a/CodenameOne/src/com/codename1/system/DefaultCrashReporter.java b/CodenameOne/src/com/codename1/system/DefaultCrashReporter.java index 1a39f7c2a0..07dd4b79f2 100644 --- a/CodenameOne/src/com/codename1/system/DefaultCrashReporter.java +++ b/CodenameOne/src/com/codename1/system/DefaultCrashReporter.java @@ -43,7 +43,7 @@ * * @author Shai Almog */ -public class DefaultCrashReporter implements CrashReport { +public final class DefaultCrashReporter implements CrashReport { private static String errorText = "The application encountered an error, do you wish to report it?"; private static String sendButtonText = "Send"; private static String dontSendButtonText = "Don't Send"; diff --git a/CodenameOne/src/com/codename1/system/NativeLookup.java b/CodenameOne/src/com/codename1/system/NativeLookup.java index 8ffef44b24..1adc4152ff 100644 --- a/CodenameOne/src/com/codename1/system/NativeLookup.java +++ b/CodenameOne/src/com/codename1/system/NativeLookup.java @@ -38,7 +38,7 @@ * * @author Shai Almog */ -public class NativeLookup { +public final class NativeLookup { /** * Indicates whether stack traces should be printed when lookup fails */ diff --git a/CodenameOne/src/com/codename1/testing/DeviceRunner.java b/CodenameOne/src/com/codename1/testing/DeviceRunner.java index 04c5405b7a..503e47336f 100644 --- a/CodenameOne/src/com/codename1/testing/DeviceRunner.java +++ b/CodenameOne/src/com/codename1/testing/DeviceRunner.java @@ -23,6 +23,7 @@ package com.codename1.testing; import com.codename1.io.Log; +import com.codename1.io.Util; import com.codename1.ui.CN; import com.codename1.ui.Display; @@ -48,8 +49,10 @@ public void runTests() { failedTests = 0; passedTests = 0; Log.p("-----STARTING TESTS-----"); + InputStream is = null; + DataInputStream di = null; try { - InputStream is = DeviceRunner.class.getResourceAsStream("/tests.dat"); + is = DeviceRunner.class.getResourceAsStream("/tests.dat"); if (is == null) { is = Display.getInstance().getResourceAsStream(null, "/tests.dat"); @@ -60,7 +63,7 @@ public void runTests() { System.exit(2); return; } - DataInputStream di = new DataInputStream(is); + di = new DataInputStream(is); int version = di.readInt(); if (version > VERSION) { Log.p("Tests were built with a new version of Codename One and can't be executed with this runner"); @@ -72,13 +75,16 @@ public void runTests() { for (int iter = 0; iter < tests.length; iter++) { tests[iter] = di.readUTF(); } - di.close(); + Util.cleanup(di); - for (int iter = 0; iter < tests.length; iter++) { - runTest(tests[iter]); + for (String test : tests) { + runTest(test); } } catch (IOException err) { TestReporting.getInstance().logException(err); + } finally { + Util.cleanup(is); + Util.cleanup(di); } TestReporting.getInstance().testExecutionFinished(getClass().getName()); if (failedTests > 0) { diff --git a/CodenameOne/src/com/codename1/testing/TestUtils.java b/CodenameOne/src/com/codename1/testing/TestUtils.java index 8c71c872de..cdd5c002a1 100644 --- a/CodenameOne/src/com/codename1/testing/TestUtils.java +++ b/CodenameOne/src/com/codename1/testing/TestUtils.java @@ -49,7 +49,7 @@ * * @author Shai Almog */ -public class TestUtils { +public final class TestUtils { private static boolean verbose; private TestUtils() { diff --git a/CodenameOne/src/com/codename1/ui/BrowserComponent.java b/CodenameOne/src/com/codename1/ui/BrowserComponent.java index afc234a438..eeefc7bd05 100644 --- a/CodenameOne/src/com/codename1/ui/BrowserComponent.java +++ b/CodenameOne/src/com/codename1/ui/BrowserComponent.java @@ -1601,7 +1601,7 @@ public void putClientProperty(String key, Object value) { * @return true if debug mode was activated */ public boolean isDebugMode() { - return getClientProperty("BrowserComponent.firebug") == Boolean.TRUE; + return Boolean.TRUE.equals(getClientProperty("BrowserComponent.firebug")); } /** diff --git a/CodenameOne/src/com/codename1/ui/Component.java b/CodenameOne/src/com/codename1/ui/Component.java index 5b6492e080..90edce8d74 100644 --- a/CodenameOne/src/com/codename1/ui/Component.java +++ b/CodenameOne/src/com/codename1/ui/Component.java @@ -6312,7 +6312,7 @@ protected void refreshTheme(String id, boolean merge) { } } else { unSelectedStyle = null; - unSelectedStyle = getUnselectedStyle(); + getUnselectedStyle(); selectedStyle = null; disabledStyle = null; pressedStyle = null; diff --git a/CodenameOne/src/com/codename1/ui/Display.java b/CodenameOne/src/com/codename1/ui/Display.java index 45d8ebd0b5..d9c1db5ac5 100644 --- a/CodenameOne/src/com/codename1/ui/Display.java +++ b/CodenameOne/src/com/codename1/ui/Display.java @@ -506,9 +506,8 @@ public static void init(Object m) { * Notice that minimize (being a Codename One method) MUST be invoked before invoking this method! */ public static void deinitialize() { - - INSTANCE.codenameOneRunning = false; synchronized (lock) { + INSTANCE.codenameOneRunning = false; lock.notifyAll(); } } @@ -4684,6 +4683,7 @@ public String getDatabasePath(String databaseName) { * Sets the frequency for polling the server in case of polling based push notification * * @param freq the frequency in milliseconds + * @deprecated we no longer support push polling */ public void setPollingFrequency(int freq) { impl.setPollingFrequency(freq); diff --git a/CodenameOne/src/com/codename1/ui/FontImage.java b/CodenameOne/src/com/codename1/ui/FontImage.java index 6933e30e64..500f33e577 100644 --- a/CodenameOne/src/com/codename1/ui/FontImage.java +++ b/CodenameOne/src/com/codename1/ui/FontImage.java @@ -74,7 +74,7 @@ * * @author Shai Almog */ -public class FontImage extends Image { +public final class FontImage extends Image { /** * Material design icon font character code see * https://www.material.io/resources/icons/ for full list diff --git a/CodenameOne/src/com/codename1/ui/Image.java b/CodenameOne/src/com/codename1/ui/Image.java index 1edf86f690..a3b8ca96ad 100644 --- a/CodenameOne/src/com/codename1/ui/Image.java +++ b/CodenameOne/src/com/codename1/ui/Image.java @@ -563,7 +563,17 @@ && pack(buf, 4, 2, false) == 0) { // Skip other markers. try { - is.skip(length); + long remaining = length; + while (remaining > 0) { + long skipped = is.skip(remaining); + if (skipped > 0) { + remaining -= skipped; + } else if (read(is, buf, 1)) { + remaining--; + } else { + return 0; + } + } } catch (IOException ex) { return 0; } diff --git a/CodenameOne/src/com/codename1/ui/InterFormContainer.java b/CodenameOne/src/com/codename1/ui/InterFormContainer.java index a9016e5040..ff2c95d650 100644 --- a/CodenameOne/src/com/codename1/ui/InterFormContainer.java +++ b/CodenameOne/src/com/codename1/ui/InterFormContainer.java @@ -22,7 +22,6 @@ */ package com.codename1.ui; -import com.codename1.ui.ComponentSelector.Filter; import com.codename1.ui.geom.Dimension; import com.codename1.ui.layouts.BorderLayout; @@ -71,28 +70,16 @@ public static Map findCommonContainers(C final Map set1 = new HashMap(); final Map set2 = new HashMap(); final Map out = new HashMap(); - ComponentSelector.select("*", root1).filter(new Filter() { - @Override - public boolean filter(Component c) { - if (c.getClass() == InterFormContainer.class) { - set1.put(((InterFormContainer) c).content, (InterFormContainer) c); - return true; - } - return false; + for (Component c : ComponentSelector.select("*", root1)) { + if (c.getClass() == InterFormContainer.class) { + set1.put(((InterFormContainer) c).content, (InterFormContainer) c); } - - }); - ComponentSelector.select("*", root2).filter(new Filter() { - @Override - public boolean filter(Component c) { - if (c.getClass() == InterFormContainer.class) { - set2.put(((InterFormContainer) c).content, (InterFormContainer) c); - return true; - } - return false; + } + for (Component c : ComponentSelector.select("*", root2)) { + if (c.getClass() == InterFormContainer.class) { + set2.put(((InterFormContainer) c).content, (InterFormContainer) c); } - - }); + } for (Map.Entry entry : set1.entrySet()) { Component c = entry.getKey(); diff --git a/CodenameOne/src/com/codename1/ui/RunnableWrapper.java b/CodenameOne/src/com/codename1/ui/RunnableWrapper.java index 985534c41c..44c4643254 100644 --- a/CodenameOne/src/com/codename1/ui/RunnableWrapper.java +++ b/CodenameOne/src/com/codename1/ui/RunnableWrapper.java @@ -106,8 +106,8 @@ public void run() { switch (type) { case 0: internal.run(); - done = true; synchronized (Display.lock) { + done = true; Display.lock.notifyAll(); } break; diff --git a/CodenameOne/src/com/codename1/ui/UIFragment.java b/CodenameOne/src/com/codename1/ui/UIFragment.java index 7950984479..3c6547851f 100644 --- a/CodenameOne/src/com/codename1/ui/UIFragment.java +++ b/CodenameOne/src/com/codename1/ui/UIFragment.java @@ -181,7 +181,7 @@ * @author shannah * @since 7.0 */ -public class UIFragment { +public final class UIFragment { // The root element of this template diff --git a/CodenameOne/src/com/codename1/ui/URLImage.java b/CodenameOne/src/com/codename1/ui/URLImage.java index 720d90e6ee..95d298dda6 100644 --- a/CodenameOne/src/com/codename1/ui/URLImage.java +++ b/CodenameOne/src/com/codename1/ui/URLImage.java @@ -70,7 +70,7 @@ * * @author Shai Almog */ -public class URLImage extends EncodedImage { +public final class URLImage extends EncodedImage { /** * Flag used by {@link #createCachedImage(java.lang.String, java.lang.String, com.codename1.ui.Image, int) }. diff --git a/CodenameOne/src/com/codename1/ui/animations/MorphTransition.java b/CodenameOne/src/com/codename1/ui/animations/MorphTransition.java index f8eea74be6..e381609409 100644 --- a/CodenameOne/src/com/codename1/ui/animations/MorphTransition.java +++ b/CodenameOne/src/com/codename1/ui/animations/MorphTransition.java @@ -40,7 +40,7 @@ * * @author Shai Almog */ -public class MorphTransition extends Transition { +public final class MorphTransition extends Transition { private int duration; private final HashMap fromTo = new HashMap(); private CC[] fromToComponents; diff --git a/CodenameOne/src/com/codename1/ui/geom/Rectangle.java b/CodenameOne/src/com/codename1/ui/geom/Rectangle.java index 292db87755..b84a036fa9 100644 --- a/CodenameOne/src/com/codename1/ui/geom/Rectangle.java +++ b/CodenameOne/src/com/codename1/ui/geom/Rectangle.java @@ -188,16 +188,6 @@ public static void intersection(int rrX, int rrY, int rrW, int rrH, int rtx1, in tx2 -= tx1; ty2 -= ty1; - // tx2,ty2 will never overflow (they will never be - // larger than the smallest of the two source w,h) - // they might underflow, though... - if (tx2 < Integer.MIN_VALUE) { - tx2 = Integer.MIN_VALUE; - } - if (ty2 < Integer.MIN_VALUE) { - ty2 = Integer.MIN_VALUE; - } - dest.x = tx1; dest.y = ty1; dest.size.setWidth(tx2); @@ -422,16 +412,6 @@ public Rectangle intersection(int rX, int rY, int rW, int rH) { } tx2 -= tx1; ty2 -= ty1; - // tx2,ty2 will never overflow (they will never be - // larger than the smallest of the two source w,h) - // they might underflow, though... - if (tx2 < Integer.MIN_VALUE) { - tx2 = Integer.MIN_VALUE; - } - if (ty2 < Integer.MIN_VALUE) { - ty2 = Integer.MIN_VALUE; - } - return new Rectangle(tx1, ty1, tx2, ty2); } @@ -462,15 +442,6 @@ public void intersection(Rectangle input, Rectangle output) { } tx2 -= tx1; ty2 -= ty1; - // tx2,ty2 will never overflow (they will never be - // larger than the smallest of the two source w,h) - // they might underflow, though... - if (tx2 < Integer.MIN_VALUE) { - tx2 = Integer.MIN_VALUE; - } - if (ty2 < Integer.MIN_VALUE) { - ty2 = Integer.MIN_VALUE; - } tx2 = Math.max(0, tx2); ty2 = Math.max(0, ty2); output.setBounds(tx1, ty1, tx2, ty2); diff --git a/CodenameOne/src/com/codename1/ui/html/AsyncDocumentRequestHandlerImpl.java b/CodenameOne/src/com/codename1/ui/html/AsyncDocumentRequestHandlerImpl.java index 15b65a0e5d..f4c02f4b74 100644 --- a/CodenameOne/src/com/codename1/ui/html/AsyncDocumentRequestHandlerImpl.java +++ b/CodenameOne/src/com/codename1/ui/html/AsyncDocumentRequestHandlerImpl.java @@ -155,8 +155,8 @@ protected void readResponse(InputStream input) throws IOException { if (callback != null) { callback.streamReady(input, docInfo); } else { - response[0] = input; synchronized (LOCK) { + response[0] = input; LOCK.notifyAll(); } } diff --git a/CodenameOne/src/com/codename1/ui/html/HTMLComponent.java b/CodenameOne/src/com/codename1/ui/html/HTMLComponent.java index 1fe1a7aedd..5be218e7d2 100644 --- a/CodenameOne/src/com/codename1/ui/html/HTMLComponent.java +++ b/CodenameOne/src/com/codename1/ui/html/HTMLComponent.java @@ -463,13 +463,13 @@ public HTMLComponent(DocumentRequestHandler handler) { * @param font The actual Codename One font object */ public static void addFont(String fontKey, Font font) { - if (fontKey != null) { - fontKey = fontKey.toLowerCase(); - } else { + if (fontKey == null) { if (font.getCharset() != null) { throw new IllegalArgumentException("Font key must be non-null for bitmap fonts"); } + throw new IllegalArgumentException("Font key must be non-null"); } + fontKey = fontKey.toLowerCase(); fonts.put(fontKey, new HTMLFont(fontKey, font)); } diff --git a/CodenameOne/src/com/codename1/ui/html/HTMLInputFormat.java b/CodenameOne/src/com/codename1/ui/html/HTMLInputFormat.java index f6c4515e2b..58f02f4eff 100644 --- a/CodenameOne/src/com/codename1/ui/html/HTMLInputFormat.java +++ b/CodenameOne/src/com/codename1/ui/html/HTMLInputFormat.java @@ -37,7 +37,7 @@ * * @author Ofir Leitner */ -class HTMLInputFormat { +final class HTMLInputFormat { /** * The allowed literals in an input format defintion diff --git a/CodenameOne/src/com/codename1/ui/html/HTMLUtils.java b/CodenameOne/src/com/codename1/ui/html/HTMLUtils.java index 38cea32150..37bd0c72e4 100644 --- a/CodenameOne/src/com/codename1/ui/html/HTMLUtils.java +++ b/CodenameOne/src/com/codename1/ui/html/HTMLUtils.java @@ -31,7 +31,7 @@ * @author Ofir Leitner * @deprecated the HTML package is no longer used or maintained and may be removed in a future revision */ -public class HTMLUtils { +public final class HTMLUtils { /** * The char entities strings supported in XML. When a char entity is found these will be compared against first. @@ -187,7 +187,7 @@ public static String encodeString(String str) { ((c >= '0') && (c <= '9')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { encodedStr += c; - } else if ((c >= 0x80) && (c <= 0xffff)) { // UTF encoding - See http://en.wikipedia.org/wiki/UTF-8 + } else if (c >= 0x80) { // UTF encoding - See http://en.wikipedia.org/wiki/UTF-8 int firstLiteral = c / 256; int secLiteral = c % 256; if (c <= 0x07ff) { // 2 literals unicode @@ -289,4 +289,4 @@ static int getStringVal(String str, String[] options, int[] vals, int defaultVal } - \ No newline at end of file + diff --git a/CodenameOne/src/com/codename1/ui/layouts/mig/ConstraintParser.java b/CodenameOne/src/com/codename1/ui/layouts/mig/ConstraintParser.java index c6128d94f1..3c3313e7f7 100644 --- a/CodenameOne/src/com/codename1/ui/layouts/mig/ConstraintParser.java +++ b/CodenameOne/src/com/codename1/ui/layouts/mig/ConstraintParser.java @@ -969,7 +969,7 @@ public static UnitValue[] parseInsets(String s, boolean acceptPanel) { String[] insS = toTrimmedTokens(s, ' '); UnitValue[] ins = new UnitValue[4]; for (int j = 0; j < 4; j++) { - UnitValue insSz = parseUnitValue(insS[j < insS.length ? j : insS.length - 1], UnitValue.ZERO, j % 2 == 1); + UnitValue insSz = parseUnitValue(insS[j < insS.length ? j : insS.length - 1], UnitValue.ZERO, (j & 1) != 0); ins[j] = insSz != null ? insSz : PlatformDefaults.getPanelInsets(j); } return ins; diff --git a/CodenameOne/src/com/codename1/ui/layouts/mig/UnitValue.java b/CodenameOne/src/com/codename1/ui/layouts/mig/UnitValue.java index ef83274fff..db6e3906f3 100644 --- a/CodenameOne/src/com/codename1/ui/layouts/mig/UnitValue.java +++ b/CodenameOne/src/com/codename1/ui/layouts/mig/UnitValue.java @@ -33,9 +33,11 @@ * Date: 2006-sep-08 */ +import com.codename1.compat.java.util.Objects; import com.codename1.util.MathUtil; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; public final class UnitValue { @@ -240,7 +242,7 @@ private UnitValue(float value, String unitStr, int unit, boolean isHor, int oper if (oper < STATIC || oper > MID) throw new IllegalArgumentException("Unknown Operation: " + oper); - if (oper > STATIC && (sub1 == null || sub2 == null)) + if (oper > STATIC && (sub1 == null || sub2 == null)) throw new IllegalArgumentException(oper + " Operation may not have null sub-UnitValues."); this.value = value; @@ -618,6 +620,32 @@ public String getConstraintString() { } public int hashCode() { - return (int) (value * 12345) + (oper >>> 5) + unit >>> 17; + int result = 17; + result = 31 * result + Float.floatToIntBits(value); + result = 31 * result + unit; + result = 31 * result + oper; + result = 31 * result + (isHor ? 1 : 0); + result = 31 * result + (unitStr != null ? unitStr.hashCode() : 0); + result = 31 * result + (linkId != null ? linkId.hashCode() : 0); + result = 31 * result + Arrays.hashCode(subUnits); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof UnitValue)) { + return false; + } + UnitValue other = (UnitValue) obj; + return Float.compare(value, other.value) == 0 + && unit == other.unit + && oper == other.oper + && isHor == other.isHor + && Objects.equals(unitStr, other.unitStr) + && Objects.equals(linkId, other.linkId) + && Arrays.equals(subUnits, other.subUnits); } } diff --git a/CodenameOne/src/com/codename1/ui/list/FilterProxyListModel.java b/CodenameOne/src/com/codename1/ui/list/FilterProxyListModel.java index 53fa8211ee..4824597e02 100644 --- a/CodenameOne/src/com/codename1/ui/list/FilterProxyListModel.java +++ b/CodenameOne/src/com/codename1/ui/list/FilterProxyListModel.java @@ -198,7 +198,7 @@ private void mergeSort(Object[] src, int destHigh = high; low += off; high += off; - int mid = (low + high) / 2; + int mid = low + ((high - low) / 2); mergeSort(dest, src, low, mid, -off, ascending); mergeSort(dest, src, mid, high, -off, ascending); diff --git a/CodenameOne/src/com/codename1/ui/plaf/RoundBorder.java b/CodenameOne/src/com/codename1/ui/plaf/RoundBorder.java index dc15281a83..6908b03572 100644 --- a/CodenameOne/src/com/codename1/ui/plaf/RoundBorder.java +++ b/CodenameOne/src/com/codename1/ui/plaf/RoundBorder.java @@ -45,7 +45,7 @@ * * @author Shai Almog */ -public class RoundBorder extends Border { +public final class RoundBorder extends Border { private static final String CACHE_KEY = "cn1$$-rbcache"; // these allow us to have more than one border per component in cache which is important for selected/unselected/pressed values private static int instanceCounter; diff --git a/CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java b/CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java index 91d3b46ba0..a4285c06b4 100644 --- a/CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java +++ b/CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java @@ -48,7 +48,7 @@ * * @author Shai Almog */ -public class RoundRectBorder extends Border { +public final class RoundRectBorder extends Border { private static final String CACHE_KEY = "cn1$$-rrbcache"; // these allow us to have more than one border per component in cache which is important for selected/unselected/pressed values private static int instanceCounter; @@ -886,7 +886,7 @@ private GeneralPath createShape(int shapeW, int shapeH) { x += strokePx / 2f; y += strokePx / 2f; - if (strokePx % 2 == 1) { + if ((strokePx & 1) != 0) { x += 0.5f; y += 0.5f; } diff --git a/CodenameOne/src/com/codename1/ui/plaf/UIManager.java b/CodenameOne/src/com/codename1/ui/plaf/UIManager.java index 98c405b3ef..c88228a367 100644 --- a/CodenameOne/src/com/codename1/ui/plaf/UIManager.java +++ b/CodenameOne/src/com/codename1/ui/plaf/UIManager.java @@ -1901,7 +1901,6 @@ Style createStyle(String id, String prefix, boolean selected) { style = new Style(getComponentStyle(baseStyle)); } } else { - baseStyle = null; if (selected) { style = new Style(defaultSelectedStyle); } else { diff --git a/CodenameOne/src/com/codename1/ui/spinner/DateTimeRenderer.java b/CodenameOne/src/com/codename1/ui/spinner/DateTimeRenderer.java index 6f66a7b824..143682d246 100644 --- a/CodenameOne/src/com/codename1/ui/spinner/DateTimeRenderer.java +++ b/CodenameOne/src/com/codename1/ui/spinner/DateTimeRenderer.java @@ -38,7 +38,7 @@ * @author Shai Almog * @deprecated use Picker instead */ -class DateTimeRenderer extends SpinnerRenderer { +final class DateTimeRenderer extends SpinnerRenderer { static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; diff --git a/CodenameOne/src/com/codename1/ui/util/Effects.java b/CodenameOne/src/com/codename1/ui/util/Effects.java index 9c928786fc..0602fb4846 100644 --- a/CodenameOne/src/com/codename1/ui/util/Effects.java +++ b/CodenameOne/src/com/codename1/ui/util/Effects.java @@ -36,7 +36,7 @@ * * @author Shai Almog */ -public class Effects { +public final class Effects { private Effects() { } diff --git a/CodenameOne/src/com/codename1/ui/util/UIBuilder.java b/CodenameOne/src/com/codename1/ui/util/UIBuilder.java index 671cca335d..ab47d7d08b 100644 --- a/CodenameOne/src/com/codename1/ui/util/UIBuilder.java +++ b/CodenameOne/src/com/codename1/ui/util/UIBuilder.java @@ -926,7 +926,7 @@ private Object readCustomPropertyValue(DataInputStream in, Class type, String ty if (type == Date.class) { boolean b = in.readBoolean(); if (b) { - return new Date(in.readInt()); + return new Date(in.readLong()); } } diff --git a/CodenameOne/src/com/codename1/util/CallbackDispatcher.java b/CodenameOne/src/com/codename1/util/CallbackDispatcher.java index da3d3b2031..e6743cdb8d 100644 --- a/CodenameOne/src/com/codename1/util/CallbackDispatcher.java +++ b/CodenameOne/src/com/codename1/util/CallbackDispatcher.java @@ -29,7 +29,7 @@ * * @author shannah */ -public class CallbackDispatcher implements Runnable { +public final class CallbackDispatcher implements Runnable { private SuccessCallback success; private FailureCallback failure; private T arg; diff --git a/CodenameOne/src/com/codename1/util/DateUtil.java b/CodenameOne/src/com/codename1/util/DateUtil.java index c130cb65b6..3d6799e994 100644 --- a/CodenameOne/src/com/codename1/util/DateUtil.java +++ b/CodenameOne/src/com/codename1/util/DateUtil.java @@ -447,7 +447,7 @@ public String getTimeAgo(Date date) { return "Just now"; } //Minutes else { - int minutes = Math.round(time_elapsed / 60); + int minutes = (int) Math.round(time_elapsed / 60.0d); if (minutes <= 60) { if (minutes == 1) { @@ -457,7 +457,7 @@ public String getTimeAgo(Date date) { } } //Hours else { - int hours = Math.round(time_elapsed / 3600); + int hours = (int) Math.round(time_elapsed / 3600.0d); if (hours <= 24) { if (hours == 1) { return "An hour ago"; @@ -466,7 +466,7 @@ public String getTimeAgo(Date date) { } } //Days else { - int days = Math.round(time_elapsed / 86400); + int days = (int) Math.round(time_elapsed / 86400.0d); if (days <= 7) { if (days == 1) { return "Yesterday"; @@ -475,7 +475,7 @@ public String getTimeAgo(Date date) { } } //Weeks else { - int weeks = Math.round(time_elapsed / 604800); + int weeks = (int) Math.round(time_elapsed / 604800.0d); if (weeks <= 4.3) { if (weeks == 1) { return "A week ago"; @@ -484,7 +484,7 @@ public String getTimeAgo(Date date) { } } //Months else { - int months = Math.round(time_elapsed / 2600640); + int months = (int) Math.round(time_elapsed / 2600640.0d); if (months <= 12) { if (months == 1) { return "A month ago"; @@ -493,7 +493,7 @@ public String getTimeAgo(Date date) { } } //Years else { - int years = Math.round(time_elapsed / 31207680); + int years = (int) Math.round(time_elapsed / 31207680.0d); if (years == 1) { return "1 year ago"; } else { diff --git a/CodenameOne/src/com/codename1/util/EasyThread.java b/CodenameOne/src/com/codename1/util/EasyThread.java index 67b4acb5c3..1de655a29b 100644 --- a/CodenameOne/src/com/codename1/util/EasyThread.java +++ b/CodenameOne/src/com/codename1/util/EasyThread.java @@ -35,7 +35,7 @@ * * @author Shai Almog */ -public class EasyThread { +public final class EasyThread { private static final List globalErrorListenenrs = new ArrayList(); private final Object LOCK = new Object(); private List errorListenenrs; diff --git a/CodenameOne/src/com/codename1/util/TBigInteger.java b/CodenameOne/src/com/codename1/util/TBigInteger.java index 8f2a34baf5..7bc5b03868 100644 --- a/CodenameOne/src/com/codename1/util/TBigInteger.java +++ b/CodenameOne/src/com/codename1/util/TBigInteger.java @@ -924,7 +924,7 @@ public int hashCode() { return hashCode; } for (int i = 0; i < digits.length; i++) { - hashCode = (hashCode * 33 + (digits[i] & 0xffffffff)); + hashCode = (hashCode * 33 + digits[i]); } hashCode = hashCode * sign; return hashCode; @@ -1240,7 +1240,7 @@ public TBigInteger modPow(TBigInteger exponent, TBigInteger m) { } TBigInteger base = this; - if (m.isOne() | (exponent.sign > 0 & base.sign == 0)) { + if (m.isOne() || (exponent.sign > 0 && base.sign == 0)) { return TBigInteger.ZERO; } if (exponent.sign == 0) { diff --git a/CodenameOne/src/com/codename1/util/TBitLevel.java b/CodenameOne/src/com/codename1/util/TBitLevel.java index d7e8e9ade1..39fc7fbe75 100644 --- a/CodenameOne/src/com/codename1/util/TBitLevel.java +++ b/CodenameOne/src/com/codename1/util/TBitLevel.java @@ -33,7 +33,7 @@ * All operations are provided in immutable way, and some in both mutable and * immutable. */ -class TBitLevel { +final class TBitLevel { /** * Just to denote that this class can't be instantiated. diff --git a/CodenameOne/src/com/codename1/util/TConversion.java b/CodenameOne/src/com/codename1/util/TConversion.java index 2f0e04ff14..35c74a79dd 100644 --- a/CodenameOne/src/com/codename1/util/TConversion.java +++ b/CodenameOne/src/com/codename1/util/TConversion.java @@ -21,7 +21,7 @@ * Static library that provides {@link TBigInteger} base conversion from/to any * integer represented in an {@link java.lang.String} Object. */ -class TConversion { +final class TConversion { /** * Holds the maximal exponent for each radix, so that radixdigitFitInInt[radix] diff --git a/CodenameOne/src/com/codename1/util/TElementary.java b/CodenameOne/src/com/codename1/util/TElementary.java index 6be5b7747b..1ef7e3e597 100644 --- a/CodenameOne/src/com/codename1/util/TElementary.java +++ b/CodenameOne/src/com/codename1/util/TElementary.java @@ -28,7 +28,7 @@ * In addition to this, some Inplace (mutable) methods are * provided. */ -class TElementary { +final class TElementary { /** * Just to denote that this class can't be instantiated diff --git a/CodenameOne/src/com/codename1/util/TLogical.java b/CodenameOne/src/com/codename1/util/TLogical.java index 659c0c5bb1..6c549ef635 100644 --- a/CodenameOne/src/com/codename1/util/TLogical.java +++ b/CodenameOne/src/com/codename1/util/TLogical.java @@ -28,7 +28,7 @@ *
  • xor
  • * */ -class TLogical { +final class TLogical { /** * Just to denote that this class can't be instantiated. diff --git a/CodenameOne/src/com/codename1/util/TMathContext.java b/CodenameOne/src/com/codename1/util/TMathContext.java index 7297722a0d..7bce39085e 100644 --- a/CodenameOne/src/com/codename1/util/TMathContext.java +++ b/CodenameOne/src/com/codename1/util/TMathContext.java @@ -137,6 +137,7 @@ public TMathContext(String val) { int i; // Index of charVal int j; // Index of chRoundingMode int digit; // It will contain the digit parsed + this.precision = 0; if ((charVal.length < 27) || (charVal.length > 45)) { throw new IllegalArgumentException("bad string format"); diff --git a/CodenameOne/src/com/codename1/util/TMultiplication.java b/CodenameOne/src/com/codename1/util/TMultiplication.java index 57e36f1696..e31f0b0e14 100644 --- a/CodenameOne/src/com/codename1/util/TMultiplication.java +++ b/CodenameOne/src/com/codename1/util/TMultiplication.java @@ -20,7 +20,7 @@ /** * Static library that provides all multiplication of {@link TBigInteger} methods. */ -class TMultiplication { +final class TMultiplication { /** * Break point in digits (number of {@code int} elements) diff --git a/CodenameOne/src/com/codename1/util/TPrimality.java b/CodenameOne/src/com/codename1/util/TPrimality.java index c619da000e..02c720bccc 100644 --- a/CodenameOne/src/com/codename1/util/TPrimality.java +++ b/CodenameOne/src/com/codename1/util/TPrimality.java @@ -23,7 +23,7 @@ /** * Provides primality probabilistic methods. */ -class TPrimality { +final class TPrimality { /** * All prime numbers with bit length lesser than 10 bits. diff --git a/maven/core-unittests/pmd.xml b/maven/core-unittests/pmd.xml new file mode 100644 index 0000000000..b261165849 --- /dev/null +++ b/maven/core-unittests/pmd.xml @@ -0,0 +1,10 @@ + + + PMD rules for core unit tests. + + + + diff --git a/maven/core-unittests/pom.xml b/maven/core-unittests/pom.xml index d176fdf7eb..1096be32c0 100644 --- a/maven/core-unittests/pom.xml +++ b/maven/core-unittests/pom.xml @@ -97,6 +97,9 @@ false false 1.8 + + ${project.basedir}/pmd.xml + diff --git a/maven/core-unittests/spotbugs-exclude.xml b/maven/core-unittests/spotbugs-exclude.xml index 83d432d54b..2034340f51 100644 --- a/maven/core-unittests/spotbugs-exclude.xml +++ b/maven/core-unittests/spotbugs-exclude.xml @@ -43,6 +43,13 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +