title: java.lang.StringIndexOutOfBoundsException: when using PolyUtil.decode
body: <p>I am relatively new to Android and I am currently working on an android project. Where I have stumbled onto a problem regarding decoding a google-map encrypted map-String. Where as short strings work fine, and long strings crash it bad.</p>
  
  <p>This is my method for decoding the map-string.</p>
  
  <pre><code>public static List&lt;LatLng&gt; decode2(String encoded) {
  
      List&lt;LatLng&gt; track = new ArrayList&lt;&gt;();
      int index = 0;
      int lat = 0, lng = 0;
  
      while (index &lt; encoded.length()) {
          int b, shift = 0, result = 0;
          do {
              b = encoded.charAt(index++) - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlat = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lat += dlat;
  
          shift = 0;
          result = 0;
          do {
              b = encoded.charAt(index++) - 63; // Error at this line.
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          int dlng = ((result &amp; 1) != 0 ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lng += dlng;
  
          LatLng p = new LatLng( (double)lat/1E5, (double)lng/1E5 );
          track.add(p);
      }
      return track;
  }
  </code></pre>
  
  <p>I call my method in this manner:</p>
  
  <pre><code>latLngArrayList = (ArrayList&lt;LatLng&gt;) PolyUtil.decode2(routes.get(groupPosition).getEncryptedMapString());
  </code></pre>
  
  <p>And get the following error on the call.</p>
  
  <pre><code>java.lang.StringIndexOutOfBoundsException: length=4438; index=4438;
      at com.google.maps.android.PolyUtil.decode2(PolyUtil.java:314)
  </code></pre>
  
  <p>The string in question:</p>
  
  <pre><code>argtJ}vg`A{F}Cw@aAi@eAy@oCmBeJWw@mBqDi@}Aw@aEoA_Iu@wC_BoDwB{CmAwB}@iAsBkBeAmAeE}Cg@MeCoB{CsDuJwMgHcG_E{DoHoKaByAq@_@iB]}CQu@@s@NyH~Bk@?k@O{AaBeAgBmDsD{CaEoD{HwE}IoDiGwCqEgB_D{H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@vBk@t@]To@Na@?}IeA}BMuBR{FlAw@Vq@n@[h@uAbEy@jAaAZuFn@mD~@}B`AaJvEeAH_ASyH_GqAm@y@M_B\\aDvB{Ah@wI[mI{@eABm@PWLs@t@e@hAmAtFsBdG_@xA]`CWnDWtAi@dAe@Vw@Ea@_@}DeHsAqCuAgDeA_ByAsA}Ag@gAAuBT}AO_NcEw@c@uAoA
  </code></pre>
  
  <p>I'm currently trying to check if it's an escape-char issue, but does not seem to work with the methods I'm currently trying.
  Any tips how to resolve this would be much appreciated.</p>