| 1 |
/* |
| 2 |
* Copyright (C) 2010 Wayne Meissner |
| 3 |
* |
| 4 |
* This file is part of jnr. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package jnr.netdb; |
| 20 |
|
| 21 |
import jnr.ffi.Platform; |
| 22 |
import jnr.ffi.Pointer; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Collections; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
/** |
| 28 |
* Utility class for native strings |
| 29 |
*/ |
| 30 |
class StringUtil { |
| 31 |
|
| 32 |
public static final List<String> getNullTerminatedStringArray(Pointer ptr) { |
| 33 |
// If it is an empty list, do not allocate an empty ArrayList for it |
| 34 |
if (ptr == null || ptr.getPointer(0) == null) { |
| 35 |
return Collections.emptyList(); |
| 36 |
} |
| 37 |
|
| 38 |
final int pointerSize = ptr.getRuntime().addressSize(); |
| 39 |
|
| 40 |
List<String> array = new ArrayList<String>(); |
| 41 |
|
| 42 |
Pointer p; |
| 43 |
for (int off = 0; (p = ptr.getPointer(off)) != null; off += pointerSize) { |
| 44 |
array.add(p.getString(0)); |
| 45 |
} |
| 46 |
|
| 47 |
return array; |
| 48 |
} |
| 49 |
} |