Skip to content

Commit

Permalink
Merge pull request #32186 from vespa-engine/balder/avoid-java21-this-…
Browse files Browse the repository at this point in the history
…warning-2

Avoid this escape.
  • Loading branch information
baldersheim authored Aug 20, 2024
2 parents 1ecef93 + 642b34d commit b9fb52d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 61 deletions.
64 changes: 26 additions & 38 deletions fsa/src/main/java/com/yahoo/fsa/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,56 @@
import java.nio.CharBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;

import com.yahoo.fsa.FSA;


/**
* Class for accessing meta-data (dat-files) used by FSA applications.
*
* @author <a href="mailto:[email protected]">Peter Boros</a>
**/
public class MetaData {

private boolean _ok = false;
private MappedByteBuffer _header;
private MappedByteBuffer _data;
private Charset _charset;
private final boolean _ok;
private final MappedByteBuffer _header;
private final MappedByteBuffer _data;
private final Charset _charset;


public MetaData(String filename){
init(filename, "utf-8");
this(filename, "utf-8");
}

public MetaData(String filename, String charsetname){
init(filename, charsetname);
}

public boolean isOk(){
return _ok;
}

private void init(String filename, String charsetname){

public MetaData(String filename, String charsetname) {
boolean ok = false;
MappedByteBuffer header = null;
MappedByteBuffer data = null;
_charset = Charset.forName(charsetname);

FileInputStream file;
try {
file = new FileInputStream(filename);
try (FileInputStream file = new FileInputStream(filename)) {
header = file.getChannel().map(MapMode.READ_ONLY,0,256);
header.order(ByteOrder.LITTLE_ENDIAN);
if (h_magic()!=-2025936501){
System.out.print("MetaData bad magic " + h_magic() +"\n");
} else {
data = file.getChannel().map(MapMode.READ_ONLY, 256, h_size());
data.order(ByteOrder.LITTLE_ENDIAN);
ok = true;
}
}
catch (FileNotFoundException e) {
System.out.print("MetaData file " + filename + " not found.\n");
return;
}

try {
_header = file.getChannel().map(MapMode.READ_ONLY,0,256);
_header.order(ByteOrder.LITTLE_ENDIAN);
if(h_magic()!=-2025936501){
System.out.print("MetaData bad magic " + h_magic() +"\n");
return;
}
_data = file.getChannel().map(MapMode.READ_ONLY,
256,
h_size());
_data.order(ByteOrder.LITTLE_ENDIAN);
_ok=true;
}
catch (IOException e) {
System.out.print("MetaData IO exception.\n");
return;
}
_ok = ok;
_header = header;
_data = data;
}

public boolean isOk() {
return _ok;
}

private int h_magic(){
Expand Down
29 changes: 6 additions & 23 deletions fsa/src/main/java/com/yahoo/fsa/topicpredictor/TopicPredictor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@
import java.util.logging.Logger;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;

import com.yahoo.fsa.FSA;
import com.yahoo.fsa.MetaData;
Expand All @@ -27,7 +17,7 @@
*
* @author Peter Boros
*/
public class TopicPredictor extends MetaData {
public final class TopicPredictor extends MetaData {

private static final String packageName = "com.yahoo.fsa.topicpredictor";

Expand Down Expand Up @@ -75,19 +65,12 @@ public List<PredictedTopic> getPredictedTopics(String segment, int maxTopics) {

int segIdx = getSegmentIndex(segment);
int[][] topicArr = getTopicArray(segIdx, maxTopics);
int numTopics = topicArr.length;
int allTopics = getNumTopics(segIdx);
/*Logger.getLogger(packageName).
fine("Segment: '" + segment + "' has " + allTopics +
" topics in automaton, fetched " + numTopics);
*/
for(int i=0; i < numTopics; i++) {
int weight = topicArr[i][1];
String[] topicInfo= getTopicInfo(topicArr[i][0]);
for (int[] ints : topicArr) {
int weight = ints[1];
String[] topicInfo = getTopicInfo(ints[0]);
String topic = topicInfo[0];
String vector= topicInfo[1];
PredictedTopic pt =
new PredictedTopic(topic, (double)weight, vector);
String vector = topicInfo[1];
PredictedTopic pt = new PredictedTopic(topic, weight, vector);
predictedTopics.add(pt);
}

Expand Down

0 comments on commit b9fb52d

Please sign in to comment.