Java Zero Format zero(byte[] bytes, int off, int len)

Here you can find the source of zero(byte[] bytes, int off, int len)

Description

Zeroes all bytes between off (inclusive) and off + len (exclusive) in the given array.

License

Apache License

Declaration

static void zero(byte[] bytes, int off, int len) 

Method Source Code

//package com.java2s;
/*//from  w  ww.  j  av a2s.c o m
 * Copyright 2013 Google Inc.
 *
 * 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.
 */

public class Main {
    private static final int ARRAY_LEN = 8192;
    private static final byte[] ZERO_ARRAY = new byte[ARRAY_LEN];

    /**
     * Zeroes all bytes between off (inclusive) and off + len (exclusive) in the given array.
     */
    static void zero(byte[] bytes, int off, int len) {
        // this is significantly faster than looping or Arrays.fill (which loops), particularly when
        // the length of the slice to be zeroed is <= to ARRAY_LEN (in that case, it's faster by a
        // factor of 2)
        int remaining = len;
        while (remaining > ARRAY_LEN) {
            System.arraycopy(ZERO_ARRAY, 0, bytes, off, ARRAY_LEN);
            off += ARRAY_LEN;
            remaining -= ARRAY_LEN;
        }

        System.arraycopy(ZERO_ARRAY, 0, bytes, off, remaining);
    }
}

Related

  1. zero(byte[] a, int aoffset, int len)
  2. zero(byte[]... arrays)
  3. zero(double[] zero)
  4. zero(int bits)
  5. zero(int x)