package tctable; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Calendar; import java.util.TimeZone; public class Tools { public static String readStringFromInputStream(InputStream inputStream) { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; try { while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } } catch (IOException e1) { e1.printStackTrace(); } // StandardCharsets.UTF_8.name() > JDK 7 try { return result.toString("UTF-8"); } catch (UnsupportedEncodingException e) { } return ""; } //Преобразовать арабские и индийские в современные цифры public static String numConvert(String str) { if(str==null) return null; String persian = "۰۱۲۳۴۵۶۷۸۹"; String arabic = "٩٨٧٦٥٤٣٢١٠"; String num = "0123456789"; //Заменяю персидские for(int i=0;i> pos)); } public static String readUTF8_1(InputStream handle) throws IOException { byte[] tmp=new byte[handle.read()]; handle.read(tmp); return new String(tmp, "UTF8"); } public static float readFloat(DataInputStream InStream) throws IOException { float f; int ch1, ch2, ch3, ch4, count; ch1 = InStream.readUnsignedByte(); ch2 = InStream.readUnsignedByte(); ch3 = InStream.readUnsignedByte(); ch4 = InStream.readUnsignedByte(); count = (ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1; f = Float.intBitsToFloat(count); return f; } public static int readInt(DataInputStream InStream) throws IOException { int ch1, ch2, ch3, ch4, count; ch1 = InStream.readUnsignedByte(); ch2 = InStream.readUnsignedByte(); ch3 = InStream.readUnsignedByte(); ch4 = InStream.readUnsignedByte(); count = (ch4 << 24) | (ch3 << 16) | (ch2 << 8) | ch1; return count; } public static short readShort(DataInputStream InStream) throws IOException { int ch1, ch2, count; ch1 = InStream.readUnsignedByte(); ch2 = InStream.readUnsignedByte(); count = (ch2 << 8) | ch1; return (short)count; } public static int readUShort(InputStream InStream) throws IOException { int ch1, ch2; ch1 = InStream.read(); ch2 = InStream.read(); return (ch2 << 8) | ch1; } public static String afterLast(String str, String ch) { int i=str.lastIndexOf(ch); if(i!=-1) { return str.substring(i+ch.length()); } return ""; } public static String beforeLast(String str, String ch) { int i=str.lastIndexOf(ch); if(i!=-1) { return str.substring(0,i); } return ""; } public static String beforeFirst(String str, String ch) { int i=str.indexOf(ch); if(i!=-1) { return str.substring(0,i); } return ""; } //узнать точку пересичений 2х линай если x=0 и y=0 то не пересиклась public static Point getCrossingLine(Point PHead0, Point PTail0, Point PHead1, Point PTail1) { Point rezPoint = new Point(); double a0, b0, c0, a1, b1, c1; boolean bRez = true; a0 = PTail0.y - PHead0.y; b0 = PHead0.x - PTail0.x; c0 = PTail0.x * PHead0.y - PHead0.x * PTail0.y; a1 = PTail1.y - PHead1.y; b1 = PHead1.x - PTail1.x; c1 = PTail1.x * PHead1.y - PHead1.x * PTail1.y; if (b1 == 0) rezPoint.x = PHead1.x;//если перпендикулярна oy else rezPoint.x = (-(b0 * c1 / b1) + c0) / ((b0 * a1 / b1) - a0); if (a1 == 0) rezPoint.y = PHead1.y;//если перпендикулярна oy else rezPoint.y = (-(c1 * a0 / a1) + c0) / ((a0 * b1 / a1) - b0); //проверка на вхождение в отрезоки (с погрешностью 0.0000001 (зачем понадобилась погрешность?)) //по x if ((rezPoint.x < Math.min(PHead0.x, PTail0.x) - 0.0000001) || (rezPoint.x > Math.max(PHead0.x, PTail0.x) + 0.0000001)) bRez = false; if ((rezPoint.x < Math.min(PHead1.x, PTail1.x) - 0.0000001) || (rezPoint.x > Math.max(PHead1.x, PTail1.x) + 0.0000001)) bRez = false; //по y if ((rezPoint.y < Math.min(PHead0.y, PTail0.y) - 0.0000001) || (rezPoint.y > Math.max(PHead0.y, PTail0.y) + 0.0000001)) bRez = false; if ((rezPoint.y < Math.min(PHead1.y, PTail1.y) - 0.0000001) || (rezPoint.y > Math.max(PHead1.y, PTail1.y) + 0.0000001)) bRez = false; if (!bRez) { rezPoint.x = 0; rezPoint.y = 0; } return rezPoint; } public static Point getCrossingLine2(Point PHead0,Point PTail0,Point PHead1,Point PTail1) { boolean bRez=true; Point rezPoint = new Point(); rezPoint.x=0; rezPoint.y=0; double a0,b0,c0,a1,b1,c1; a0=PTail0.y-PHead0.y; b0=PHead0.x-PTail0.x; c0=PTail0.x*PHead0.y-PHead0.x*PTail0.y; a1=PTail1.y-PHead1.y; b1=PHead1.x-PTail1.x; c1=PTail1.x*PHead1.y-PHead1.x*PTail1.y; if (b1==0) rezPoint.x=PHead1.x;//если перпендикулярна oy else rezPoint.x=(-(b0*c1/b1)+c0)/((b0*a1/b1)-a0); if (a1==0) rezPoint.y=PHead1.y;//если перпендикулярна ox else rezPoint.y=(-(c1*a0/a1)+c0)/((a0*b1/a1)-b0); //по x if (rezPoint.xMath.max(PHead0.x,PTail0.x)) bRez=false; if (rezPoint.xMath.max(PHead1.x,PTail1.x)) bRez=false; //по y if (rezPoint.yMath.max(PHead0.y,PTail0.y)) bRez=false; if (rezPoint.yMath.max(PHead1.y,PTail1.y)) bRez=false; if (!bRez) { rezPoint.x=0; rezPoint.y=0; } return rezPoint; } //Так как в replaceAll много заморочек с регулярными выражениями public static String replaceAll(String txt, String val, String rep) { if(txt==null || val==null || rep==null) return txt; return txt.replace(val,rep); /*while(true) { String tmpstr=txt.replace(val, rep); if(tmpstr.equals(txt)) { txt=tmpstr; break; }else { txt=tmpstr; } } return txt;*/ } public static byte[] subArray(byte[] b, int offset, int length) { byte[] sub = new byte[length]; for (int i = offset; i < offset + length; i++) { try { sub[i - offset] = b[i]; } catch (Exception e) { } } return sub; } }