/*
 * Copyright 2007 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.zxing;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * Utility class containing back-ported constructions.<br>
 * This class is used to implement syntactic sugar and utility methods found in
 * newer Java versions, but lacking in the (older) Android world.<br>
 * These methods will throw {@link NullPointerException} when a <code>null</code>
 * arguments are used. This is on-par with the JVM crashing when calling a method
 * on a <code>null</code> reference.
 * 
 * @author Lachezar Dobrev
 */
public class Backport {
  private Backport() {
    // Static methods holder class.
  }

  /** Replacement for Arrays.copyOf(). */
  public static int[] copyOf(int[] source, int offset) {
    if (source == null) {
      throw new NullPointerException("copyOf(null, ...)");
    }
    if (offset < 0 || offset >= source.length) {
      throw new ArrayIndexOutOfBoundsException(offset);
    }
    int[] target = new int[source.length - offset];
    System.arraycopy(source, offset, target, 0, target.length);
    return target;
  }

  /** Replacement for String.getBytes(Charset). */
  public static byte[] getBytes(String contents, Charset charset) {
    if (contents == null) {
      throw new NullPointerException("getBytes(null, ...)");
    }
    if (charset == null) {
      throw new NullPointerException("getBytes(..., null)");
    }
    ByteBuffer output;
    output = charset.encode(contents);
    if (output.hasArray()) {
      return output.array(); // Hope this is the case.
    }

    byte[] ba;
    ba = new byte[output.remaining()];

    for (int i = 0; i < ba.length; i++) {
      ba[i] = output.get();
    }
    return ba;
  }

  /** Replacement for new String(byte[], Charset). */
  public static String getString(byte[] content, Charset charset) {
    if (content == null)
      throw new NullPointerException("getString(null, ...)");
    if (charset == null)
      throw new NullPointerException("getString(..., null)");
    return charset.decode(ByteBuffer.wrap(content)).toString();
  }

  /** Replacement for String.isEmpty(). */
  public static boolean isEmpty(String contents) {
    if (contents == null) {
      throw new NullPointerException("isEmpty(null)");
    }
    return contents.length() == 0;
  }

}

@@ -1310,219 +1310,17 @@
 int 
-offset) %7B%0A    if (source == null) %7B%0A      throw new NullPointerException(%22copyOf(null, ...)%22);%0A    %7D%0A    if (offset %3C 0 %7C%7C offset %3E= source.length) %7B%0A      throw new ArrayIndexOutOfBoundsException(offset);%0A    %7D
+length) %7B
 %0A   
@@ -1347,30 +1347,14 @@
 int%5B
-source.length - offset
+length
 %5D;%0A 
@@ -1385,14 +1385,9 @@
 ce, 
-offset
+0
 , ta
@@ -1399,22 +1399,40 @@
  0, 
-target.
+Math.min(source.length, 
 length)
+)
 ;%0A  

Fix bug in Backport.copyOf(int[],int): argument is length, not offset. Fixes one test case error. Core has 1 test failure in Aztec encoding. Check possible encoding issues.