Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while(true) loop without break

Tags:

I am quite new to Java programming. For now I am studying source code of an android app called Evercam. However, I have a problem understanding a part of the code which involves while(true) loop.

Here is the snippet of the code:

while (true)
{
    while (true)
    {
        byte[] responseMessageByteArray = new byte[4000];
        DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray, responseMessageByteArray.length);
        datagramSocket.receive(datagramPacketRecieve);
        String responseMessage = new String(datagramPacketRecieve.getData());
        EvercamDiscover.printLogMessage("\nResponse Message:\n" + responseMessage);
        StringReader stringReader = new StringReader(responseMessage);
        InputNode localInputNode = NodeBuilder.read(stringReader);
        EnvelopeProbeMatches localEnvelopeProbeMatches = (EnvelopeProbeMatches)(new Persister()).read(EnvelopeProbeMatches.class, localInputNode);
        if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.size() > 0)
        {
            ProbeMatch localProbeMatch = (ProbeMatch) localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.get(0);
            if (uuidArrayList.contains(localProbeMatch.EndpointReference.Address))
            {
                EvercamDiscover.printLogMessage("ONVIFDiscovery: Address " + localProbeMatch.EndpointReference.Address + " already added");
            }
            else
            {
                uuidArrayList.add(localProbeMatch.EndpointReference.Address);
                DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);
                if (discoveredCamera.hasValidIpv4Address())
                {
                    this.onActiveOnvifDevice(discoveredCamera);
                    cameraList.add(discoveredCamera);
                }
            }
        }
    }
}

Doesn't this create an infinite loop? My fundamentals in Java isn't strong, so I would be so grateful if anyone can tell in in what instances will a while(true){//codes} actually exits without any break or does it ever exit??


EDIT

My bad for actually extracting this snippet from decompiling directly from the android project files. I did not know that it would be different, and then again, I know very little. Here is the original code:

    public ArrayList<DiscoveredCamera> probe() {
ArrayList<DiscoveredCamera> cameraList = new ArrayList<DiscoveredCamera>();

try {
    DatagramSocket datagramSocket = new DatagramSocket();
    datagramSocket.setSoTimeout(SOCKET_TIMEOUT);
    InetAddress multicastAddress = InetAddress.getByName(PROBE_IP);

    if (multicastAddress == null) {
    // System.out.println("InetAddress.getByName() for multicast returns null");
    return cameraList;
    }

    // Send the UDP probe message
    String soapMessage = getProbeSoapMessage();
    // System.out.println(soapMessage);
    byte[] soapMessageByteArray = soapMessage.getBytes();
    DatagramPacket datagramPacketSend = new DatagramPacket(
        soapMessageByteArray, soapMessageByteArray.length,
        multicastAddress, PROBE_PORT);
    datagramSocket.send(datagramPacketSend);

    ArrayList<String> uuidArrayList = new ArrayList<String>();
    while (true) {
    // System.out.println("Receiving...");
    byte[] responseMessageByteArray = new byte[4000];
    DatagramPacket datagramPacketRecieve = new DatagramPacket(
        responseMessageByteArray,
        responseMessageByteArray.length);
    datagramSocket.receive(datagramPacketRecieve);

    String responseMessage = new String(
        datagramPacketRecieve.getData());

    EvercamDiscover.printLogMessage("\nResponse Message:\n"
        + responseMessage);

    StringReader stringReader = new StringReader(responseMessage);
    InputNode localInputNode = NodeBuilder.read(stringReader);
    EnvelopeProbeMatches localEnvelopeProbeMatches = new Persister()
        .read(EnvelopeProbeMatches.class, localInputNode);
    if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches
        .size() <= 0) {
        continue;
    }

    ProbeMatch localProbeMatch = localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches
        .get(0);
    // EvercamDiscover.printLogMessage("Probe matches with UUID:\n"
    // +
    // localProbeMatch.EndpointReference.Address + " URL: " +
    // localProbeMatch.XAddrs);
    if (uuidArrayList
        .contains(localProbeMatch.EndpointReference.Address)) {
        EvercamDiscover.printLogMessage("ONVIFDiscovery: Address "
            + localProbeMatch.EndpointReference.Address
            + " already added");
        continue;
    }
    uuidArrayList.add(localProbeMatch.EndpointReference.Address);
    DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);

    if (discoveredCamera.hasValidIpv4Address()) {
        onActiveOnvifDevice(discoveredCamera);
        cameraList.add(discoveredCamera);
    }
    }
} catch (Exception e) {
    // ONVIF timeout. Don't print anything.
}

Turns out there is continue statement in the actual code. Thank you so much for the response, I will remember that de-compiled classes should not be depended on.

like image 368
daisura99 Avatar asked Jul 29 '16 09:07

daisura99


1 Answers

This looks like an infinite loop. To be absolutely sure, you would have to statically read every statement and follow invoked methods to see if any possible invocations like Activity#finish() or Service#stopSelf() exists which would finish the currently running activity, effectively breaking the loop.

Another possibility is that the code is intended to be running in an infinite loop as a background thread service, and some other component would have an option to kill that service when it reaches a certain condition. For example, it could be part of a Runnable class and executed in a thread pool, and when a timeout exists, the pool is shut down.

like image 105
M A Avatar answered Oct 03 '22 02:10

M A