JavaCAD
ObjFile.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package eu.mihosoft.vrl.v3d;
7 
8 import java.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.nio.charset.StandardCharsets;
12 import java.nio.file.Path;
13 import java.nio.file.Paths;
14 
15 // TODO: Auto-generated Javadoc
21 public final class ObjFile {
22 
24  private String obj;
25 
27  private final String mtl;
28 
30  private InputStream objStream;
31 
33  private InputStream mtlStream;
34 
36  static final String MTL_NAME = "$JCSG_MTL_NAME$";
37 
44  ObjFile(String obj, String mtl) {
45  this.obj = obj;
46  this.mtl = mtl;
47  }
48 
55  public void toFiles(Path p) throws IOException {
56 
57  Path parent = p.getParent();
58 
59  String fileName = p.getFileName().toString();
60 
61  if (fileName.toLowerCase().endsWith(".obj")
62  || fileName.toLowerCase().endsWith(".mtl")) {
63  fileName = fileName.substring(0, fileName.length() - 4);
64  }
65 
66  String objName = fileName + ".obj";
67  String mtlName = fileName + ".mtl";
68 
69  obj = obj.replace(MTL_NAME, mtlName);
70  objStream = null;
71 
72  if (parent == null) {
73  FileUtil.write(Paths.get(objName), obj);
74  FileUtil.write(Paths.get(mtlName), mtl);
75  } else {
76  FileUtil.write(Paths.get(parent.toString(), objName), obj);
77  FileUtil.write(Paths.get(parent.toString(), mtlName), mtl);
78  }
79 
80  }
81 
87  public String getObj() {
88  return this.obj;
89  }
90 
96  public String getMtl() {
97  return this.mtl;
98  }
99 
105  public InputStream getObjStream() {
106  if (objStream == null) {
107  objStream = new ByteArrayInputStream(obj.getBytes(StandardCharsets.UTF_8));
108  }
109 
110  return objStream;
111  }
112 
118  public InputStream getMtlStream() {
119  if (mtlStream == null) {
120  mtlStream = new ByteArrayInputStream(mtl.getBytes(StandardCharsets.UTF_8));
121  }
122 
123  return mtlStream;
124  }
125 }
static void write(Path p, String s)
Definition: FileUtil.java:63
InputStream getObjStream()
Definition: ObjFile.java:105
InputStream getMtlStream()
Definition: ObjFile.java:118